Laravel Auth :: attempt()失败。我用SQLite btw

时间:2015-01-16 09:15:57

标签: authentication laravel

我做错了什么?

<?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)
}

不确定我应该添加哪些信息。

应用程序/配置/ auth.php

'driver' => 'eloquent',
'model'  => 'User',
'table'  => 'users',

应用程序/配置/ app.php

'key'    => 'DMmiPAxSYz4O2jG44S92OcdPZN7ZsGGs',
'cipher' => MCRYPT_RIJNDAEL_256,

模型/ 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);
        }
    }

}

2 个答案:

答案 0 :(得分:0)

这里有一些你可以做的检查

  • 您是否设置了config/auth.php驱动程序,型号和表格?
  • 您是否填写了用户模型的fillable数组?
  • 您是否更改了key内的config/app.php

还尝试使用dd($ m)来查看从该查询中获得的内容。

答案 1 :(得分:0)

我发现了什么问题。

第一次使用这段代码哈希密码:

User::create(array(
    'name'  => $user_name,
    'email' => $user_name.'@test.com',
    'password' => Hash::make('123123'), // <---- first time
));

用户模型中的这个mutator在将密码输入数据库之前第二次进行哈希处理:

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
));
相关问题