Hash::check() return false in laravel 5
我只是从laravel 5开始,我正在做一个简单的登录功能,以检查用户传递的电子邮件和密码是否与数据库中存储的电子邮件和密码匹配。我一直在阅读文档([https://laravel.com/docs/5.0/hashing1),但是
创建新用户时,我会像这样对密码进行哈希处理:
1 2 3 | $content = json_decode($request->getContent(), true); $user -> password = Hash::make($content['email']); |
我的登录功能如下:
1 2 3 4 5 6 7 8 9 10 11 12 | public function login(Request $request) { $content = json_decode($request -> getContent(), true); $user = DB::table('users')->where('email', $content['email'])->first(); if (Hash::check($content['password'], $user->{'password'})) { // Redirect to dashboard } } |
谢谢!!
实际上,您是在创建用户时对电子邮件进行散列而不是对密码进行散列。从
更改代码
1 | $user->password = Hash::make($content['email']); |
致
1 | $user->password = Hash::make($content['password']); |
Facade Hash只会加密您的数据:
1 | Hash::make('123456'); |
与以下内容相同:
1 | $password = bcrypt('123456'); |
要登录用户,您需要使用AuthController函数:
1 | Auth::attempt(['email' => '[email protected]' , 'password' => Hash::make('password')]); |
这是一个例子。
如果您收到请求,则可以添加以下方法进行登录:
1 2 3 4 | if(Auth::attempt(['email' => $request->email, 'password' => $request->password , 'active' => 1])){ flash()->success('Successfully logged in!'); return redirect('/'); } |
尝试功能将对您的密码字段进行哈希处理,并将其与数据库数据进行比较。
我想到了同样的问题。检查数据库用户表,密码字段。将字段的大小设置为60或更大。这个固定的地雷。