Aurora Vollvik
Aurora Vollvik

Reputation: 175

Laravel: How can I authenticate a user by e-mail, where the e-mail address is stored in a separate table?

I'm creating a website using Laravel and I wish to use the Auth middleware. To let users add multiple e-mail addresses and phone numbers, so they have backup options for recovery etc., I'm storing them in separate tables from the main users table.

Looking closer at the Authentication documentation and skimming through some of the classes used for the authentication, I'm unsure how I might specify that the e-mail and phone number should be retrieved from their respective tables rather than the users table (email_addresses and phone_numbers).

If anyone could tell me how to set this up, or at least point me in the right direction, I would be super grateful. Don't need to have it spelled out, but if someone know which files I need to edit that would help a lot.

Upvotes: 1

Views: 1411

Answers (1)

Abhishek
Abhishek

Reputation: 3967

Get your user record by email from emails table:

$record = Emails::where('email', $email)->first();

if($record){
    //if this email is valid you will get the user record
    if(auth()->validate(['id' => $record->user_id, 'password' => $input['password']])) auth()->loginUsingId($record->user_id)
    else echo 'Invalid password';
}
else {
    echo 'User not present!'
}

Upvotes: 1

Related Questions