您好,我正在尝试重定向任何尝试登录且具有 ban_status 的用户! = 0
我想知道对我来说最好的方法是什么,我想只是将它们注销并将它们重定向到禁止的页面,但我只想禁止用户访问此页面,或者在表单上添加一个错误来说明“你被禁止了”等。这是我当前的代码:
公共(public)函数登录(请求$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 ($lockedOut = $this->hasTooManyLoginAttempts($request)) {
$this->fireLockoutEvent($request);
return $this->sendLockoutResponse($request);
}
$credentials = $this->credentials($request);
if ($this->guard()->attempt($credentials, $request->has('remember'))) {
if ($this->guard()->user()->ban_status === 0) { // Check if the user is banned or not before allowing login
return $this->sendLoginResponse($request);
}
else {
$this->guard()->logout();
$request->session()->flush();
$request->session()->regenerate();
return redirect('/banned'); // If the user banned_status is set to 1, then they will be redirected to a banned page.
}
}
// 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.
if (! $lockedOut) {
$this->incrementLoginAttempts($request);
}
return $this->sendFailedLoginResponse($request);
}
最佳答案
如果用户知道您的路线名称,该方法不会阻止用户直接访问您的其他路线。
您可以通过添加中间件来阻止这种情况,保护所有其他路由,如果中间件无法验证,则将用户重定向到被禁止的页面。
关于php - Laravel 5.3 在登录时重定向被禁止的用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39316758/