修改 Laravel 的 Auth 的 login 方法
因为Auth
的login
方法不能验证is_active
字段, 或者验证不能正确的提示错误消息(或者有我不知道的方法)
我的代码是这样的
public function login(Request $request)
{
$this->validateLogin($request);
// 多次登录失败上锁
if ($this->hasTooManyLoginAttempts($request)) {
$this->fireLockoutEvent($request);
return $this->sendLockoutResponse($request);
}
// 试图登录
if ($this->attemptLogin($request)) {
// 获取当前登录用户
$user = $this->guard()->user();
// 如果用户没有激活 !!! TODO
if (! $this->userRepository->isActive($user))
{
// 退出登录
Auth::logout();
return back()->withInput()->withErrors([$this->username() => '账户未激活']);
}
return $this->sendLoginResponse($request);
}
// 如果登录尝试不成功,我们将增加次数
// 登录并将用户重定向到登录表单。当然,当这
// 用户超过他们的尝试,他们将被锁定的最大数量。
$this->incrementLoginAttempts($request);
return $this->sendFailedLoginResponse($request);
}
我在原生的验证登录之后才做了判断是都为激活用户, 如果不是,然后使用Auth::logout()
注销用户 (我使用 $this->guard()->login($user) 不能注销,也望回答一下)。
但感觉多做了一步,可以可以在未登录时验证一下? 用 Auth
的方法有吗?
https://learnku.com/docs/laravel/5.5/authentication#指定额外条件
@IceBay 这个我也看过,但是语言解决的问题是,当返回错误的时候我怎么才能知道是用户密码错误还是用户未激活?