Iraklis Iraklis - 1 month ago 18
PHP Question

Auth Attempt password Laravel

In one project i am registering a user like this

private function insertUserCredentials(User $user, Request $request){
...
$user->email = $request->email
$user->password = Hash::make($request->password );
...
}


And I login like this

if (!Auth::attempt([
'email' => $request->emailLogin,
'password' => $request->passwordLogin,
], $remember)
)
...


The password is inserted in db with hasing but in attemt i just have to retrieve the password the user has typed and everything works.

Now, if I register a user like this

$Blowfish_Pre = '$2a$05$';
$Blowfish_End = '$';
$Allowed_Chars ='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789./';
$Chars_Len = 63;
$Salt_Length = 21;
$salt = "";
for($i=0; $i<$Salt_Length; $i++)
{
$salt .= $Allowed_Chars[mt_rand(0,$Chars_Len)];
}
$bcrypt_salt = $Blowfish_Pre . $salt . $Blowfish_End;
$user->email = $request->registeremail;
$user->password = crypt($request->password, $bcrypt_salt);


how can i use the Auth attempt? What do I have to change to make it work?

if (!Auth::attempt([
'email' => $request->emailLogin,
'password' => **here is my question**,
], $remember)
)

Answer

I guess easiest way to do that is to create your own method for that and authenticate user manually by using login() method:

public function customAttempt($username, $password) {
    $user = User::find('username', $username);
    if (....checking for credentials.....) {
        // Authorize user.
        Auth::login($user, true);
    }
}
Comments