在Login Controller中重写的经过身份验证的方法不起作用

时间:2018-05-22 19:02:23

标签: php laravel function laravel-5

我试图覆盖登录控制器中经过身份验证的方法但不知何故它不起作用。我只是试着简单地用dd();但它仍然没有用。

以下是我的功能代码:

   <my-Directive></my-Directive>

我实际想做的事情如下,但为了简单起见,我有dd();在功能中。

public function authenticated(Request $request, $user)
{
    dd("hi");
}

整个控制器:

public function authenticated(Request $request, $user)
{
    if (!$user->verified) {
        auth()->logout();
        return back()->with('warning', 'You need to confirm your account. We have sent you an activation code, please check your email.');
    }
    return redirect()->intended($this->redirectPath());
}

请忽略控制器中经过身份验证的函数中的额外注释代码。

1 个答案:

答案 0 :(得分:7)

那是因为你要覆盖登录功能,因此永远不会调用经过身份验证的功能。

如果你看一下这个特性:

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);
}

如您所见,函数sendLoginResponse是调用authenticated函数的函数。

protected function sendLoginResponse(Request $request)
{
    $request->session()->regenerate();

    $this->clearLoginAttempts($request);

    return $this->authenticated($request, $this->guard()->user())
            ?: redirect()->intended($this->redirectPath());
}

因此,在您的情况下,它应该是这样的,重新生成会话并清除尝试:

return $this->sendLoginResponse($request);

或者,如果您想直接跳到经过身份验证的功能:

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

你的功能应如下所示:

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');
    }
}