Auth::logoutOtherDevices 导致密码错误问题
注:经 [@ALMAS](https://learnku.com/users/7036) 提醒,发现user模型中有定义一个修改器,用于密码处理,问题出在该处,并非laravel 问题。特此纠正,感谢 [@ALMAS](https://learnku.com/users/7036) 的提醒。
代码如下:
public function setPasswordAttribute($value)
{
$value = str_replace(' ', '', $value);
$this->attributes['password'] = bcrypt($value);
}
以上纠正于:2019年7月16日 20:32分!
最近做开发的时候,要求实现单设备登录功能,看了下文档,发现 Auth::logoutOtherDevices($password) 正好满足条件。于是兴冲冲的使用了该方法。结果悲剧了, 账号登录后,Auth::logoutOtherDevices 处理完,再想登录的时候,报密码错误。找问题发现使用 Auth::logoutOtherDevices 后,数据库中的密码变了,用 password_verify 去验证数据库中改变后的密码,死活验证不通过。 好嘛,只能跟踪源码找问题了。
/**
* Invalidate other sessions for the current user.
*
* The application must be using the AuthenticateSession middleware.
*
* @param string $password
* @param string $attribute
* @return bool|null
*/
public function logoutOtherDevices($password, $attribute = 'password')
{
if (! $this->user()) {
return;
}
$result = tap($this->user()->forceFill([
**$attribute => Hash::make($password),**
]))->save();
if ($this->recaller() ||
$this->getCookieJar()->hasQueued($this->getRecallerName())) {
$this->queueRecallerCookie($this->user());
}
$this->fireOtherDeviceLogoutEvent($this->user());
return $result;
}
在该方法中,发现密码是Hash::make加密后存储。刚开始以为是这里加密方式问题,后来经测试发现不是。真正的原因是密码被加密了两次,$attribute => Hash::make($password) 加密了一次,在入库的时候又做了一次hash,最终导致密码验证不通过。找到问题就简单了,直接改成如下代码,问题解决。
$result = tap($this->user()->forceFill([
**$attribute => $password,**
]))->save();
初次发文,请多关照!
本作品采用《CC 协议》,转载必须注明作者和本文链接
推荐文章: