栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

仅当用户使用Laravel处于活动状态时登录

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

仅当用户使用Laravel处于活动状态时登录

Laravel 5.4 / 5.5

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

public function login(IlluminateHttpRequest $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 toredirect elsewhere



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/466058.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号