skip to Main Content

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',
        ]);

Phpmyadmin

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


  1. Chosen as BEST ANSWER

    I changed to 'password' => 'password', in create method and also changed User class to class User extends Authenticatable

    Now it's working as it should.


  2. 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

    User::factory()->create([
        'name' => 'Admin User',
        'login' => 'admin',
        'password' => 'password',
        'role' => 'Admin',
    ]);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search