I use Hash to bcrypt password in my seeder, and in my controller, I use Auth to check request from client, but it always returns false, I don't know why.
This is the code:
My HomeController:
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);
}
}
}
My seeder has:
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")
]);
My model User:
<?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',
];
}
and my router has:
Route::post('/login', 'HomeController@login');
but when I request with info of seeder, Auth::attempt ()
always return false.
Remove Hash::make
from password capture field. When you use Auth::attempt
it will get hash value by default and search through DB.
Try
$password = $request->input('password'); # remove Hash::make
if( Auth::attempt(['mssv' =>$username, 'pass' => $password])) { # remove quotes