Login only if user is active using Laravel

前端 未结 19 1845
孤城傲影
孤城傲影 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条回答
  •  -上瘾入骨i
    2020-11-30 23:33

    Most logical, and clean, is to handle this within the validateLogin method.

    LoginController.php (Laravel 6.x)

    /**
     * Validate the user login request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return void
     *
     * @throws \Illuminate\Validation\ValidationException
     */
    protected function validateLogin(Request $request)
    {
        // Get the user details from database and check if email is verified.
        $user = User::where('username', '=', $request->input($this->username()))->first();
        if ($user->email_verified_at == NULL) {
            throw ValidationException::withMessages([$this->username() => __('auth.failed_login_missing_email_verification')]);
        }
    
        // Email is verified, validate input.
        return $request->validate([
            $this->username() => 'required|string',
            'password' => 'required|string',
        ]);
    }
    

提交回复
热议问题