我使用了laravel 5.1身份验证进行登录。但是Auth::attempt($credentials)
总是返回false!
我使用以下路线创建用户:
Route::get('newuser', function () {
return User::create([
'username' => 'admin',
'email' => 'admin@gmail.com',
'password' => Hash::make('123'),
]);
});
我添加了一个简单的postLogin函数来覆盖AuthController中的这个函数:
public function postLogin(Request $request) {
$credentials = $this->getCredentials($request);
if(Auth::attempt($credentials)) {
return 'ok';
}
return 'nok';
}
getCredentials是具有以下内容的laravel函数:
protected function getCredentials(Request $request)
{
return $request->only($this->loginUsername(), 'password');
}
答案 0 :(得分:0)
这件事可能有多种原因。 我建议你检查用户表格中phpMyAdmin中密码列的长度。
将密码列更改为存储密码的表中数据库中的最大长度。
另外我建议您尝试使用以下代码,如果它正常工作。
public function postLogin()
{
// Login credentials
$credentials = array(
'username' => 'admin@gmail.com',
'password' => '123',
);
// Authenticate the user
if (Auth::attempt($credentials))
{
return 'ok';
}
else{
return "failed";
}
希望它会有所帮助。
不要忘记更改数据库中的密码列长度,将其更改为最大值。
将密码数据类型更改为LONGTEXT,长度为maximun或1000.
最后也很重要的是删除表中的所有记录并重新启动进程,因为已经保存的私有保存已经与另一个长度一起保存,并且因为currunt哈希密码与旧密码不匹配。
因此截断该表并再次尝试。