A Simple Laravel Package for handling multiple authentication
A Simple Laravel Package for handling multiple authentication, (*2)
Check working sample code of this project here, (*3)
Open your terminal and navigate to your laravel folder. Now run the following command, (*4)
composer require sarav/laravel-multiauth
or, (*5)
"require": { "sarav/laravel-multiauth": "^0.0.7" }
Replace "Illuminate\Auth\AuthServiceProvider::class" with "Sarav\Multiauth\MultiauthServiceProvider::class", (*6)
Modify auth.php file from the config directory to something like this, (*7)
'multi' => [ 'user' => [ 'driver' => 'eloquent', 'model' => App\User::class, 'table' => 'users' ], 'admin' => [ 'driver' => 'eloquent', 'model' => App\Admin::class, 'table' => 'admins' ] ],
Note : I have set second user as admin here. Feel free to change yours but don't forget to add its respective driver, model and table., (*8)
We are done! Now you can simply login user/admin like the following code, (*9)
\Auth::loginUsingId("user", 1); // Login user with id 1 \Auth::loginUsingId("admin", 1); // Login user with id 1 // Attempts to login user with email id johndoe@gmail.com \Auth::attempt("user", ['email' => 'johndoe@gmail.com', 'password' => 'password']); // Attempts to login admin with email id johndoe@gmail.com \Auth::attempt("admin", ['email' => 'johndoe@gmail.com', 'password' => 'password']);
Simply pass the first parameter as key which you have configured in auth.php to perform authentication for either user or admin., (*10)
Now you can pass Guard class easily through nice "with" function., (*11)
$auth = $auth->with('admin');
For more information check out this article., (*12)