I have this code
public function login(){
$user = User::find(1);
if ($user){
echo "I can connect to database. ";
$UserPassword = $user->password;
$UserLogin = $user->login;
echo "UserLogin = $UserLogin; UserPassword = $UserPassword; ";
}
if(Auth::attempt(['login'=>$UserLogin,'password'=>$UserPassword])){
echo "Alright!";
}
else
{
echo "Why it doesn't work?";
}
}
When I run it, this function print me "I can connect to database. UserLogin = admin; UserPassword = 123; Why it doesn't work?" I do not understand why does not happen Auth :: attempt event
You're doing the following:
However that won't work because...
attempt
takes a username (eg: admin
) and a plain text password (eg: 123
). The user database stores the hashed password (eg: $2a$10$xbFFpiHRRv8y6e.6STPQxehcLwY8V9Lq/HUNjfbuCdHSe2PTLBcNy
) which means you're passing the hashed password to attempt
, which will always fail.
If you want to login a user by their ID (eg: 1
) use loginUsingId
:
Auth::loginUsingId(1);
If you want to login a user using a provided username and password use attempt
, eg:
$username = 'admin';
$password = '123';
Auth::attempt(['username' => $username, 'password' => $password]);
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community