Laravel-哈希检查即使正确也返回false

时间:2019-02-01 09:30:42

标签: laravel hash bcrypt

我正在使用md5从表单登录,并且尝试切换到bcrypt,但是Hashk::check方法始终返回false,即使密码正确,也知道为什么它不起作用?

         $email = Input::get('email');
         $password =  Input::get('password');
         $user = User::where("email","=",$email)->first();

            if(Hash::check($password,$user->password)) {

                    $userID = $user->id_user;
                    $username = $user->first_name." ".$user->last_name;
                    $admin = "yes";
                    Session::put('userID',$userID);
                    Session::put('userName',$username);
                    Session::put('admin',$admin);
                    return redirect('/cw-admin/');
            } else {
                    return Redirect::to('/cw-admin/login')
                        ->withErrors(['no' => 'Incorrect password']);
            }
            }

EDIT添加了User模型,我只更改了可填充对象,primaryKey和ID,方法保留为空。

class User extends Model implements Authenticatable
{

    protected $table = 'user';
    protected $primaryKey = 'id_user';

    protected $fillable = [
        'first_name','last_name','password','email'
    ];

    protected $hidden = [
        'password', 'remember_token',
    ];
    }

dd输出:

User {#592 ▼
  #table: "user"
  #primaryKey: "id_user"
  #fillable: array:4 [▼
    0 => "first_name"
    1 => "last_name"
    2 => "password"
    3 => "email"
  ]
  #hidden: array:2 [▼
    0 => "password"
    1 => "remember_token"
  ]
  #connection: "mysql"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #attributes: array:7 [▶]
  #original: array:7 [▶]
  #changes: []
  #casts: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  +timestamps: true
  #visible: []
  #guarded: array:1 [▼
    0 => "*"
  ]
}

1 个答案:

答案 0 :(得分:2)

您必须检查User表定义。如果“散列”字段即使在其中发布了内容也返回false,则它不会检索与您发布的值相同的值。

就我而言,我将在存储哈希值的列上使用最大长度。

Alter Table Users Modify password VCHAR(255);