Multiple Email Addresses
Setup

Setup

In order to use this feature, update your \App\Models\User::class model to implement the HasEmailAddresses interface and use the HasEmailAddresses trait like the following:

https://github.com/yottahq/laravel-example-project/blob/master/app/Models/User.php#L12
<?php
namespace App\Models;
 
//...
use YottaHQ\LaravelExtendedAuth\Contracts\HasEmailAddresses;
 
class User extends Authenticatable implements HasEmailAddresses
{
    use \YottaHQ\LaravelExtendedAuth\Traits\HasEmailAddresses;
    //...
}

Now you have linked the two models and can do for example:

Create new email address:
$user->emailAddresses()->create([
    'email' => request('email'),
]);
Get all the user email addresses:
$emails = $user->emailAddresses;

Using the user provider for authentication

This package comes with a custom user provider https://github.com/yottahq/laravel-extended-auth/blob/main/src/Drivers/ExtendedUserProvider.php which is similar to Laravel's user provider but instead it's able to authenticate users with their secondary emails as well.

Update your config/auth.php file and change your providers.users.driver to laravelExtendedAuth like in the example project:

https://github.com/yottahq/laravel-example-project/blob/master/config/auth.php#L64
'providers' => [
    'users' => [
        'driver' => 'laravelExtendedAuth',
        'model' => App\Models\User::class,
    ],
 
    // ...
]
ℹ️

Authenticating users with their secondary email addresses will only work if you set the authenticate_by_any_email configuration option to true.

Last updated on December 25, 2022