由于laravel 5.3自带的auth
没有邮件激活的机制,需要增加邮件激活,所以对它进行了一点改写:
laravel 5.3自带的auth的login方法原本是这样的:
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);
}
$credentials = $this->credentials($request);
//问题是关于这个if判断。
if ($this->guard()->attempt($credentials, $request->has('remember'))) {
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);
}
问题在上面if()判断那里。
改写描述:
在user表中用is_confirmed
字段来表示帐号是否激活,0代表未激活,1代表已激活,激活才能登陆。
如果帐号没激活,在视图中单独显示帐号没有激活,而不是笼统的显示帐号或密码不对,或帐号没激活,所以把原本的代码改成了这样:(只改了if语句那里)
if($this->guard()->attempt($credentials)){
if ($this->guard()->attempt(array_merge($credentials,['is_confirmed'=>1]), $request->has('remember'))) {
return $this->sendLoginResponse($request);
}elseif($this->guard()->attempt(array_merge($credentials,['is_confirmed'=>0]), $request->has('remember'))){
\Session::flash('email_not_confirmed','帐号没有激活,无法登录,请先去您的邮箱激活帐号');
return back()->withInput($request->only($this->username(), 'remember'));
}
}
问题:
感觉我的代码逻辑有点重复,有其他更优化的写法吗?