Support the ongoing development of Laravel.io →
posted 10 years ago
Authentication

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

Last updated 2 years ago.
0

You're doing the following:

  1. Fetching a user from the database
  2. Assigning their username and hashed password to variables
  3. Attempting to log the user in using their hashed password

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

http://laravel.com/docs/security#authenticating-users

Last updated 2 years ago.
0

Many thanks for your help!

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Faydaen faydaen Joined 21 Apr 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.