Error during Auth::attempt($credentials)
. Followed https://laravel.com/docs/11.x/authentication#authenticating-users
I’ve created simple seeder:
$hashedPassword = Hash::make('password');
User::factory()->create([
'name' => 'Admin User',
'login' => 'admin',
'password' => $hashedPassword,
'role' => 'Admin',
]);
I am trying to log in user with
public function loginUser(Request $request)
{
$credentials = $request->only('login', 'password');
if (Auth::attempt($credentials)) {
$request->session()->regenerate();
return redirect()->intended('/');
}
return back()->withErrors(['login' => 'Invalid login or password']); // Redirect back with error message
}
I am getting error even if password in DB is hashed. Did I miss something ?
2
Answers
I changed to
'password' => 'password',
in create method and also changed User class toclass User extends Authenticatable
Now it's working as it should.
I think your bug is because the password is encrypted by default and you re-encrypt it with a specific encryption.
Use this code and it will probably work