laravel - Laravel 5.5 中的 Auth::attempt () 总是返回 false
问题描述
我在播种机中使用哈希来加密密码,在我的控制器中,我使用 Auth 来检查来自客户端的请求,但它总是返回 false,我不知道为什么。
这是代码:
我的家庭控制器:
public function login(Request $request)
{
$username = $request->input('username');
$password = $request->input('password');
if( Auth::attempt(['mssv' =>$username, 'pass' =>$password])) {
$success = new MessageBag(['successlogin' => 'welcome']);
return view('welcome')->withErrors($success);
} else {
$errors = new MessageBag(['errorlogin' => 'Login fail']);
return redirect()->back()->withInput()->withErrors($errors);
}
}
}
我的播种机有:
DB::table('sinhvien')->insert([
'hoten' => 'Nguyễn Ngọc Anh Thư',
'mssv' =>'DH51400668',
'pass' =>Hash::make('DH51400668'),
'created_at'=>date("Y-m-d H:i:s"),
'updated_at'=>date("Y-m-d H:i:s")
]);
我的模型用户:
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $table='sinhvien' ;
protected $fillable = [
'mssv', 'hoten', 'pass',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'pass', 'remember_token',
];
}
我的路由器有:
Route::post('/login', 'HomeController@login');
但是当我请求播种机的信息时,Auth::attempt ()
总是返回false。
解决方案
Hash::make
从密码捕获字段中删除。使用Auth::attempt
时默认获取哈希值并通过数据库进行搜索。
尝试
$password = $request->input('password'); # remove Hash::make
if( Auth::attempt(['mssv' =>$username, 'pass' => $password])) { # remove quotes
推荐阅读
- swift - 如何捕获闭包的返回值
- sql - SQL 查找满足至少一个条件的元组列表
- ruby-on-rails - 渲染具有未知/无限嵌套的分层列表视图
- python - 检测到缺失字符时无法将空白行添加到熊猫
- php - Brew 安装 php,但“php -v”返回 MacOS 原生 php 的信息
- sqlite - 当我运行查询时,SQLite 全文搜索给出 0 行
- c# - 如何在抽象类c#中设置值
- bash - 即使一切都很好,为什么 ssh-action 的输出中有这么多 `err:` 前缀?
- sql - 无法进入 Visual Studio 中的 SQL Server 存储过程
- odata - SAP UI5:PopOver 中标题和详细信息集的 OData 绑定