Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.2 - Login with only Email

I am trying to create a Social Login with Laravel 5.2.

I am getting user-email, so I need to log-in with only that email.

So, what I have done is-

Auth::attempt(
                [
                    'email' => $user->email
                ]);

But I am getting this error-

ErrorException in EloquentUserProvider.php line 112:
Undefined index: password

More detail-

enter image description here

like image 714
Abrar Jahin Avatar asked Jan 06 '23 07:01

Abrar Jahin


2 Answers

I have solved it like this-

//Get the user
$user = User::where('email', '=', $user->email)->first();
//Now log in the user if exists
if ($user != null)
{
    Auth::loginUsingId($user->id);
}

And it is working now :).

like image 120
Abrar Jahin Avatar answered Jan 13 '23 08:01

Abrar Jahin


I appreciate that you've already answered your own question, but here's my response anyway.

It's worth noting that a password is required for a reason. By default, Laravel expects there to be a password, otherwise it's just insecure.

Rather than writing your way around the default auth implementation, why not use the Laravel package Socialite, a package made and maintained by Laravel (read: Taylor). It's quite literally part of Laravel, otherwise you're just reinventing the wheel.

https://github.com/laravel/socialite

like image 45
ollieread Avatar answered Jan 13 '23 10:01

ollieread