Gammer
Gammer

Reputation: 5608

Laravel 5.2 : Authentication auth::attempt returns false

I am working on authentication module, I have this weird problem, Auth::attempt returns false, Where my username and password is correct.

The same question is asked here also but that is not addressing my problems and that question is about laravel 4, I have tried their methods but still not working.

I have the following code in my controller:

$user = array(
    'name' => Input::get('username'),
    'password' => Input::get('password')
);
if (Auth::attempt($user)) {
    return "ok.";
} else {
    dd($user);
}  

The else part returns :

array:2 [▼
  "name" => "ali"
  "password" => "ali"
]

Which means the name and the password is correct.

I am using laravel 5.2 and in my users table the password is not hashed and there is no remember_token means i have created the user directly.

Upvotes: 1

Views: 577

Answers (2)

Yannis Berrouag
Yannis Berrouag

Reputation: 356

the attempt method hash the password before comparing it to the database, meaning that if the password in your database is not hashed, it will not match.

Upvotes: 2

Laravel User
Laravel User

Reputation: 1129

this will not work because auth::attempt converts password to hash using bcrypt, and looks for that hash in users table to match.

in short the password should be a hash stored in database table for auth::attempt to work.

that is why your if() condition failing.

below is from laravel 5.2 docs

The attempt method accepts an array of key / value pairs as its first argument. The values in the array will be used to find the user in your database table. So, in the example above, the user will be retrieved by the value of the email column. If the user is found, the hashed password stored in the database will be compared with the hashed password value passed to the method via the array. If the two hashed passwords match an authenticated session will be started for the user.

The attempt method will return true if authentication was successful. Otherwise, false will be returned.

Upvotes: 1

Related Questions