Laravel Passport Scopes

后端 未结 6 1019
灰色年华
灰色年华 2020-11-28 21:10

I am a bit confused on the laravel scopes part.

I have a user model and table.

How can I assign a user the role of user, customer and/or admin.

I hav

6条回答
  •  执笔经年
    2020-11-28 21:55

    Implement the Raymond Lagonda response and it works very well, just to be careful with the following. You need to override some methods from AuthenticatesUsers traits in ApiLoginController:

        /**
         * Send the response after the user was authenticated.
         *
         * @param  \Illuminate\Http\Request  $request
         * @return \Illuminate\Http\Response
         */
        protected function sendLoginResponse(Request $request)
        {
            // $request->session()->regenerate(); // coment this becose api routes with passport failed here.
    
            $this->clearLoginAttempts($request);
    
            return $this->authenticated($request, $this->guard()->user())
                    ?: response()->json(["status"=>"error", "message"=>"Some error for failes authenticated method"]);
    
        }
    
        /**
         * Get the failed login response instance.
         *
         * @param  \Illuminate\Http\Request  $request
         * @return \Illuminate\Http\RedirectResponse
         */
        protected function sendFailedLoginResponse(Request $request)
        {
            return response()->json([
                                    "status"=>"error", 
                                    "message"=>"Autentication Error", 
                                    "data"=>[
                                        "errors"=>[
                                            $this->username() => Lang::get('auth.failed'),
                                        ]
                                    ]
                                ]);
        }
    

    If you changed the login: username field to a custom username field eg: e_mail. You must refine the username method as in your LoginController. Also you have to redefine and edit the methods: validateLogin, attemptLogin, credentials since once the login is validated, the request is forwarded to passport and must be called username.

提交回复
热议问题