extend laravel 5 built-in authentication to login only “if user == active”

前端 未结 5 2096
春和景丽
春和景丽 2021-02-05 20:56

I use the included authentication of laravel 5.1.6 and want to know how I can extend it, to work like this:

if (Auth::attempt([\'email\' => $email, \'password         


        
5条回答
  •  一向
    一向 (楼主)
    2021-02-05 21:33

    Solved: this link ( tutorial) will help you : https://medium.com/@mshanak/solved-tutorial-laravel-5-3-disable-enable-block-user-login-web-passport-oauth-4bfb74b0c810

    step1:

    add new field to the User table called ‘status’ (1:enabled, 0:disabed)
    

    step2:

    to block the web login , in app/Http/Controllers/Auth/LoginController.php add the follwoing function:
    
    /**
     * Get the needed authorization credentials from the request.
     *
     * @param \Illuminate\Http\Request $request
     * @return array
     */
     protected function credentials(\Illuminate\Http\Request $request)
     {
     $credentials = $request->only($this->username(), ‘password’);
    
    return array_add($credentials, ‘status’, ‘1’);
     }
    

    Step3:

    to block the user when using passport authentication ( token ) , in the User.php model add the following function :
    
    public function findForPassport($identifier) {
         return User::orWhere(‘email’, $identifier)->where(‘status’, 1)->first();
         }
    

    Done :)

提交回复
热议问题