Login only if user is active using Laravel

前端 未结 19 1855
孤城傲影
孤城傲影 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:56

    Laravel 6.6 tested. Overwrite validateLogin in your LoginController.php

    use Illuminate\Http\Request;
    use App\User;
    use Illuminate\Validation\ValidationException;
    

    ......

    /**
     * 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 user is exist and active.
        $user = User::where('email',$request->email)->first();
        if( $user && !$user->activation){
            throw ValidationException::withMessages([$this->username() => __('User has been desactivated.')]);
        }
    
        // Then, validate input.
        return $request->validate([
            $this->username() => 'required|string',
            'password' => 'required|string',
        ]);
    }
    

提交回复
热议问题