编辑
#
-
Laravel版本:5.2.45
-
PHP版本:5.6
-
数据库驱动程序和版本:
描述:
嗨,
我完成了从laravel 5.1到5.2的更新并解决了一些问题......
现在我尝试登录,但是Auth验证了'password'字段的密码,但在我的db中,密码的另一个名字是'senha' .
照亮/认证/ EloquentUserProvider.php
/**
* Retrieve a user by the given credentials.
*
* @param array $credentials
* @return \Illuminate\Contracts\Auth\Authenticatable|null
*/
public function retrieveByCredentials(array $credentials)
{
if (empty($credentials)) {
return;
}
// First we will add each credential element to the query as a where clause.
// Then we can execute the query and, if we found a user, return it in a
// Eloquent User "model" that will be utilized by the Guard instances.
$query = $this->createModel()->newQuery();
foreach ($credentials as $key => $value) {
**if (! Str::contains($key, 'password'))** {
$query->where($key, $value);
}
}
return $query->first();
}
你怎么看,函数尝试在凭证中找到字符串'password'时应该是'senha'
与文件中的validateCredentials函数相同的问题是/ Auth / EloquentUserProvider.php
/**
* Validate a user against the given credentials.
*
* @param \Illuminate\Contracts\Auth\Authenticatable $user
* @param array $credentials
* @return bool
*/
public function validateCredentials(UserContract $user, array $credentials)
{
$plain = $credentials['password'];
return $this->hasher->check($plain, $user->getAuthPassword());
}
解决这个问题的最佳方法是什么?
2 回答
最好的方法是......
并在他们的模型身份验证:
正如Dayglor指出的那样,EloquentUserProvider没有密码'字段,其他任何字段都是用于查询数据库的字段 . 只有在找到用户之后才会通过执行
hash_check
检查密码 . 需要credentials['password]
对$user->getAuthPassword()
进行哈希检查 . 因此,只需在User
模型上设置getAuthPassword
方法,即可返回要用作要检查的password
字段的正确字段 .