Laravel Auth::attempt() 失败。我使用 SQLite 顺便说一句
Laravel Auth::attempt() fails. I use SQLite btw
我做错了什么?
<?php
public function login() {
$user_name = time();
User::create(array(
'name' => $user_name,
'email' => $user_name.'@test.com',
'password' => Hash::make('123123'),
));
$user = array(
'email' => $user_name.'@test.com',
'password' => '123123',
);
$m = User::where('email' , '=', $user_name.'@test.com')->first();
dd([
'Auth::attempt($user)',
Auth::attempt($user),
'Auth::check()',
Auth::check(),
'Hash::check($m->password, \'123123\')',
Hash::check($m->password, '123123')
]);
}
结果:
array(6) {
[0]=>
string(20) "Auth::attempt($user)"
[1]=>
bool(false)
[2]=>
string(13) "Auth::check()"
[3]=>
bool(false)
[4]=>
string(38) "Hash::check($user->password, '123123')"
[5]=>
bool(false)
}
不确定我应该添加什么信息。
app/config/auth.php
'driver' => 'eloquent',
'model' => 'User',
'table' => 'users',
app/config/app.php
'key' => 'DMmiPAxSYz4O2jG44S92OcdPZN7ZsGGs',
'cipher' => MCRYPT_RIJNDAEL_256,
models/User.php
<?php
use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
/**
* Validation rules
*/
public static $rules = array(
'name' => 'required',
'email' => 'email|required|unique',
'password' => 'min:6',
);
/**
* Validation rules
*/
public static $messages = array(
'name.required' => 'The name field is required',
'email.email' => 'The email field must contain properly formatted email.',
'email.required' => 'The email field is required',
'password.required' => 'The password field is required',
'password.min:6' => 'The password must be minimum 6 characters long',
);
protected $table = 'users';
protected $hidden = array('password', 'remember_token');
protected $guarded = array('id');
public function setPasswordAttribute($value) {
if ($value) {
$this->attributes['password'] = Hash::make($value);
}
}
}
好吧,这里有一些您可以做的检查
- 您是否设置了
config/auth.php
驱动程序、型号和 table?
- 用户模型的
fillable
数组你填了吗?
config/app.php
里面的key
你改了吗?
也尝试 dd($m) 以查看您从该查询中得到了什么。
我发现哪里不对了。
这部分代码第一次哈希密码:
User::create(array(
'name' => $user_name,
'email' => $user_name.'@test.com',
'password' => Hash::make('123123'), // <---- first time
));
并且用户模型中的这个修改器在将密码放入数据库之前进行了第二次哈希处理:
public function setPasswordAttribute($value) {
if ($value) {
$this->attributes['password'] = Hash::make($value); // <---- second time
}
}
所以我只是将第一个块更改为:
User::create(array(
'name' => $user_name,
'email' => $user_name.'@test.com',
'password' => '123123', // <---- no hashing here
));
我做错了什么?
<?php
public function login() {
$user_name = time();
User::create(array(
'name' => $user_name,
'email' => $user_name.'@test.com',
'password' => Hash::make('123123'),
));
$user = array(
'email' => $user_name.'@test.com',
'password' => '123123',
);
$m = User::where('email' , '=', $user_name.'@test.com')->first();
dd([
'Auth::attempt($user)',
Auth::attempt($user),
'Auth::check()',
Auth::check(),
'Hash::check($m->password, \'123123\')',
Hash::check($m->password, '123123')
]);
}
结果:
array(6) {
[0]=>
string(20) "Auth::attempt($user)"
[1]=>
bool(false)
[2]=>
string(13) "Auth::check()"
[3]=>
bool(false)
[4]=>
string(38) "Hash::check($user->password, '123123')"
[5]=>
bool(false)
}
不确定我应该添加什么信息。
app/config/auth.php
'driver' => 'eloquent',
'model' => 'User',
'table' => 'users',
app/config/app.php
'key' => 'DMmiPAxSYz4O2jG44S92OcdPZN7ZsGGs',
'cipher' => MCRYPT_RIJNDAEL_256,
models/User.php
<?php
use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
/**
* Validation rules
*/
public static $rules = array(
'name' => 'required',
'email' => 'email|required|unique',
'password' => 'min:6',
);
/**
* Validation rules
*/
public static $messages = array(
'name.required' => 'The name field is required',
'email.email' => 'The email field must contain properly formatted email.',
'email.required' => 'The email field is required',
'password.required' => 'The password field is required',
'password.min:6' => 'The password must be minimum 6 characters long',
);
protected $table = 'users';
protected $hidden = array('password', 'remember_token');
protected $guarded = array('id');
public function setPasswordAttribute($value) {
if ($value) {
$this->attributes['password'] = Hash::make($value);
}
}
}
好吧,这里有一些您可以做的检查
- 您是否设置了
config/auth.php
驱动程序、型号和 table? - 用户模型的
fillable
数组你填了吗? config/app.php
里面的key
你改了吗?
也尝试 dd($m) 以查看您从该查询中得到了什么。
我发现哪里不对了。
这部分代码第一次哈希密码:
User::create(array(
'name' => $user_name,
'email' => $user_name.'@test.com',
'password' => Hash::make('123123'), // <---- first time
));
并且用户模型中的这个修改器在将密码放入数据库之前进行了第二次哈希处理:
public function setPasswordAttribute($value) {
if ($value) {
$this->attributes['password'] = Hash::make($value); // <---- second time
}
}
所以我只是将第一个块更改为:
User::create(array(
'name' => $user_name,
'email' => $user_name.'@test.com',
'password' => '123123', // <---- no hashing here
));