E
E
EA-EKB2018-08-30 18:02:47
Laravel
EA-EKB, 2018-08-30 18:02:47

Why does Auth::attempt return false when given a username and password that exist in the database?

Good day!
I'm trying to write my own module for working with users, but I'm stuck on authorization. Everything is elementary in the documentation:

if (Auth::attempt(['login' => $request->login, 'password' => $request->password]))
    return 'Пользователь авторизован';
else
    return 'Пользователь не авторизован';

But in my case, Auth::attempt constantly returns false, even when I explicitly pass the login and password hash from the database
Auth::attempt(['login' => 'login', 'password' => 'f394h0wxfc9t54hgdc78w9f...']) <= pbkdf2-хеш пароля длинной 75 символов

Record with such login and the password in a DB is. Password hashes (I derive from the database and generated in my authorization method) match completely. Case in field names is respected.
I also encountered this problem: initially, the password field in the database had the name pass, but Auth::attempt gave an error "password not defined", adding
public function getAuthPassword() {
    return $this->pass;
}
gave nothing to the user model. How to specify your set of fields in Auth::attempt?
Authorization method:
public function login(Request $request) {
    $user = User::whereLogin($request->login)->first();
    $user_db_pass = hash_pbkdf2("sha256", $request->pass, $user->salt, 1000, 75);

    //dd(Auth::attempt(['login' => 'admin', 'password' => 'c0b6de9afb4a16e83cba60e5946a87d84a6333f7064237126c0e58aa03699d86e43965e643a']));

    if ($user->password == $user_db_pass) {
        if (Auth::attempt(['login' => $request->login, 'password' => $user_db_pass]))
            return redirect()->intended(route('users.profile'));
        else
            return $user->password.'<br>'.$user_db_pass;
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
JhaoDa, 2018-08-30
@JhaoDa

And why are you passing the password hash there instead of the password itself? If you have your own password hashing mechanism, write a wrapper for it , or don't use Auth::attemptit, because it hashes passwords inside (that's amazing!) when comparing.
PS And yes, in this context, not "authorization", but "authentication".

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question