Laravel 5 Override Login Function

前端 未结 3 1815
挽巷
挽巷 2021-01-14 17:05

I\'m working on my Laravel Project and trying to override the default postLogin() from AuthenticatesAndRegistersUsers . So I have updated my AuthController and added this t

相关标签:
3条回答
  • 2021-01-14 17:13

    I would add following first thing in postLogin() function.

           $this->validate($request, [
                'email' => 'required|email', 'password' => 'required',
            ]);
    
            if ($this->auth->validate(['email' => $request->email, 'password' => $request->password, 'status' => 0])) {
                return redirect($this->loginPath())
                    ->withInput($request->only('email', 'remember'))
                    ->withErrors('Your account is Inactive or not verified');
            }
    

    status is a flag in user table. 0 = Inactive, 1 = active. so whole function would look like following..

    public function postLogin(Request $request)
        {
            $this->validate($request, [
                'email' => 'required|email', 'password' => 'required',
            ]);
            if ($this->auth->validate(['email' => $request->email, 'password' => $request->password, 'status' => 0])) {
                return redirect($this->loginPath())
                    ->withInput($request->only('email', 'remember'))
                    ->withErrors('Your account is Inactive or not verified');
            }
            $credentials  = array('email' => $request->email, 'password' => $request->password);
            if ($this->auth->attempt($credentials, $request->has('remember'))){
                    return redirect()->intended($this->redirectPath());
            }
            return redirect($this->loginPath())
                ->withInput($request->only('email', 'remember'))
                ->withErrors([
                    'email' => 'Incorrect email address or password',
                ]);
        }
    
    0 讨论(0)
  • 2021-01-14 17:15

    With

    if ($this->auth->attempt($credentials, $request->has('remember')))
    

    you are loggin the user in so if you want to log him out use

    Auth::logout();
    

    use that piece of code in the else if statement

    0 讨论(0)
  • 2021-01-14 17:19

    Try following this discussion at laracasts.

    Here is a solution

    if ($this->guard()->validate($this->credentials($request))) {
            $user = $this->guard()->getLastAttempted();
            if ($user->is_activated && $this->attemptLogin($request)) {
                return $this->sendLoginResponse($request);
            } else {
                $this->incrementLoginAttempts($request);
                if ($request->ajax()) {
                    return response()->json([
                        'error' => 'This account is not activated.'
                    ], 401);
                }
            }
        }
    

    It provided a crucial update for a do my homework service at Top Homework Expert

    0 讨论(0)
提交回复
热议问题