overridden authenticated method in Login Controller doesn't work

前端 未结 1 1858
梦毁少年i
梦毁少年i 2021-02-11 04:44

I\'m trying to override the authenticated method in the Login Controller but somehow it isn\'t working. I just tried to simply dd(); but still it doesn\'t work.

Below is

1条回答
  •  再見小時候
    2021-02-11 05:00

    That's because you are overwriting the login function, hence the authenticated function is never called.

    If you take a look at the trait:

    public function login(Request $request)
    {
        $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);
    }
    

    As you can see, the function sendLoginResponse is the one that is calling the authenticated function.

    protected function sendLoginResponse(Request $request)
    {
        $request->session()->regenerate();
    
        $this->clearLoginAttempts($request);
    
        return $this->authenticated($request, $this->guard()->user())
                ?: redirect()->intended($this->redirectPath());
    }
    

    Therefore, in your case, it should be something like this, to regenerate the session and clear the attempts:

    return $this->sendLoginResponse($request);
    

    Or if you want to skip directly to the authenticated function:

    return $this->authenticated($request, auth()->user());
    

    And your function should look like this:

    public function login(Request $request)
    {
        if (Auth::attempt(['email' => $request->email, 'password' => $request->password, 'isActive' => '1']))
        {
            // Updated this line
            return $this->sendLoginResponse($request);
    
            // OR this one
            // return $this->authenticated($request, auth()->user());
        }
        else
        {
            return $this->sendFailedLoginResponse($request, 'auth.failed_status');
        }
    }
    

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