程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了仅当用户使用Laravel处于活动状态时登录大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决仅当用户使用Laravel处于活动状态时登录?

开发过程中遇到仅当用户使用Laravel处于活动状态时登录的问题如何解决?下面主要结合日常开发的经验,给出你关于仅当用户使用Laravel处于活动状态时登录的解决方法建议,希望对你解决仅当用户使用Laravel处于活动状态时登录有所启发或帮助;

Laravel 5.4 / 5.5

login()通过将此功能放在您的中来覆盖默认功能LoginController

public function login(\Illuminate\http\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);
    }

    // This section is the only change
    if ($this->guard()->valIDate($this->credentials($request))) {
        $user = $this->guard()->getLastAttempted();

        // Make sure the user is active
        if ($user->active && $this->attemptLogin($request)) {
            // Send the normal successful login response
            return $this->sendLoginResponse($request);
        } else {
            // Increment the Failed login attempts and redirect BACk to the
            // login form with an error message.
            $this->incrementLoginAttempts($request);
            return redirect()
                ->BACk()
                ->withinput($request->only($this->username(), 'remember'))
                ->withErrors(['active' => 'You must be active to login.']);
        }
    }

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

login()建议在此问题的许多其他答案上覆盖这种方式的方法,因为它使您仍然可以使用Laravel 5.4+的许多更高级的身份验证功能,例如登录限制,多个身份验证防护驱动程序/提供程序等。仍然允许您设置自定义错误消息

Laravel 5.3

更改或覆盖您的postLogin()函数,AuthController如下所示:

public function postLogin(request $request)
{
    $this->valIDate($request, [
        'email' => 'required|email', 'password' => 'required',
    ]);

    $credentials = $this->getCredentials($request);

    // This section is the only change
    if (Auth::valIDate($credentials)) {
        $user = Auth::getLastAttempted();
        if ($user->activE) {
            Auth::login($user, $request->has('remember'));
            return redirect()->intended($this->redirectPath());
        } else {
            return redirect($this->loginPath()) // Change this to redirect elsewhere
                ->withinput($request->only('email', 'remember'))
                ->withErrors([
                    'active' => 'You must be active to login.'
                ]);
        }
    }

    return redirect($this->loginPath())
        ->withinput($request->only('email', 'remember'))
        ->withErrors([
            'email' => $this->getFailedLoginmessage(),
        ]);

}

此代码将重定向回登录页面,并显示一条有关用户处于非活动状态的错误消息。如果要重定向到身份验证页面,则可以更改我用注释标记的行Change this to redirect elsewhere

解决方法

我目前正在使用Laravel应用程序,为了防止垃圾邮件,我决定只有活跃的用户才能登录。我目前正在使用Laravel的登录系统,就像Laravel的官方网站教程中一样,这是我的表单操作:

<form class="form-horizontal" role="form" method="POST" action="{{ url('/auth/login') }}">

完全可以正常工作,但是我想检查用户的活动状态,如果不活动,它将被重定向到激活页面,否则它将登录。有没有简单的方法可以执行此操作,或者我有义务制造新的控制器,路由和进行更多验证?谢谢。

编辑:忘记提及我的数据库中有一个“活动”列。

大佬总结

以上是大佬教程为你收集整理的仅当用户使用Laravel处于活动状态时登录全部内容,希望文章能够帮你解决仅当用户使用Laravel处于活动状态时登录所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。