[英]Laravel Auth::attemp() always false
First of all, I have seen this topics (and many others):首先,我已经看到了这个主题(以及许多其他主题):
Laravel Auth::attempt() always false? Laravel Auth::attempt() 总是假的?
Laravel 5 Auth:attempt() always returns false Laravel 5 Auth:attempt() 总是返回 false
But I have been unable to login in my app.但是我一直无法登录我的应用程序。 I'm using Laravel 5.6 and my table is called
oc_users
.我使用的是 Laravel 5.6 ,我的表名为
oc_users
。
DB::table('oc_users')->insert(
[
'email' => 'myemail@gmail.com',
'pwd' => bcrypt('123456'),
'active' => true
]);
I went to the file Laravel > App > User.php and changed to:我转到文件 Laravel > App > User.php 并更改为:
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
protected $table = 'oc_users';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['email', 'pwd'];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = ['pwd', 'remember_token'];
}
And the code to perform the login:以及执行登录的代码:
public function form_login(Request $request)
{
if($request->filled('email') == FALSE || $request->filled('pwd') == FALSE)
return Redirect::back()->withErrors('Fill the credentials');
if (Auth::attempt(['email' => $request->email, 'pwd' => $request->pwd]))
return redirect()->intended('app');
return Redirect::back()->withErrors('Email or password wrong');
}
I also went to the file App > Config > Auth.php trying to figure out if there's a table I need to change but it doesn't seem to exists in Laravel 5.6我还转到文件 App > Config > Auth.php 试图找出是否有我需要更改的表,但它似乎在 Laravel 5.6 中不存在
From what I experienced before, auth::attempt() function has a default checking of the password
column. 根据我以前的经验,auth :: attempt()函数对
password
列进行了默认检查。
To make your code work, you could try this. 为了使您的代码正常工作,您可以尝试一下。
In your user model add: 在用户模型中添加:
public function getAuthPassword() {
return $this->pwd;
}
This function will override the default getAuthPassword() function in the Authenticatable.php
此函数将覆盖
Authenticatable.php
的默认getAuthPassword()函数。
Laravel now uses bcrypt
: Laravel 现在使用
bcrypt
:
class User extends Model {
public function setPasswordAttribute($value) {
$this->attributes['password'] = bcrypt($value);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.