Login only if user is active using Laravel

前端 未结 19 1847
孤城傲影
孤城傲影 2020-11-30 23:26

I\'m currently working on my Laravel app and to prevent spam I decided that only active users are able to login. I\'m currently using Laravel\'s login system just like in La

19条回答
  •  佛祖请我去吃肉
    2020-11-30 23:39

    Laravel 5.8 tested. Put this code in your LoginController.php and be happy.

    public function login(Request $request)
    {
        $user = User::where('username',$request->username)->first();
        if( $user && !$user->active){
            return redirect()->back()->with('error','the user has been desactivated');
        }
    
        $this->validateLogin($request);
    
        // If the class is using the ThrottlesLogins trait, we can automatically throttle
        // the login attempts for this application. We'll key this by the username and
        // the IP address of the client making these requests into this application.
        if ($this->hasTooManyLoginAttempts($request)) {
            $this->fireLockoutEvent($request);
    
            return $this->sendLockoutResponse($request);
        }
    
        if ($this->attemptLogin($request)) {
            return $this->sendLoginResponse($request);
        }
    
        // If the login attempt was unsuccessful we will increment the number of attempts
        // to login and redirect the user back to the login form. Of course, when this
        // user surpasses their maximum number of attempts they will get locked out.
        $this->incrementLoginAttempts($request);
    
        return $this->sendFailedLoginResponse($request);
    }
    

提交回复
热议问题