3

Authentication Drivers / "Multi-Auth"

as prior to release of laravel 5.2 it is stated that multi auth suppots out of the box. but there is no any example codes showing how to authenticate using different drivers with routes. So I need help setting up the multi-auth using default laravel 5.2

4

3 Answers 3

6

Create two new models: App\Admin and App\User. Update config/auth.php:

return [ 'defaults' => [ 'guard' => 'user', 'passwords' => 'user', ], 'guards' => [ 'user' => [ 'driver' => 'session', 'provider' => 'user', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admin', ], ], 'providers' => [ 'user' => [ 'driver' => 'eloquent', 'model' => 'App\User', ], 'admin' => [ 'driver' => 'eloquent', 'model' => 'App\Admin', ], ], 'passwords' => [ 'user' => [ 'provider' => 'user', 'email' => 'auth.emails.password', 'table' => 'password_resets', 'expire' => 60, ], 'admin' => [ 'provider' => 'admin', 'email' => 'auth.emails.password', 'table' => 'password_resets', 'expire' => 60, ] ] ]; 

In kernel.php

 protected $middleware = [ \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class, \Illuminate\Session\Middleware\StartSession::class, \Illuminate\View\Middleware\ShareErrorsFromSession::class ]; /** * The application's route middleware groups. * * @var array */ protected $middlewareGroups = [ 'web' => [ \App\Http\Middleware\EncryptCookies::class, \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, //\App\Http\Middleware\VerifyCsrfToken::class, ], 'api' => [ 'throttle:60,1', ], ]; 

and in Route.php set below code and test

 Route::get('/login', function() { $auth = auth()->guard('admin'); $credentials = [ 'email' => '[email protected]', 'password' => 'password', ]; if ($auth->attempt($credentials)) { return redirect('/profile'); } }); Route::get('/profile', function() { if(auth()->guard('admin')->check()){ print_r(auth()->guard('admin')->user()->toArray()); } if(auth()->guard('user')->check()){ print_r(auth()->guard('user')->user()->toArray()); } }); 
Sign up to request clarification or add additional context in comments.

1 Comment

Looks good, but it seems a lot of repeating (guards, providers, passwords are really similar...)
1

Using the rajpurohit-dinesh example, we just need to finnish first step:

1: Create an App\Admin model (on our app folder). Here is how should be your Authenticatable class.

<?php namespace App; use Illuminate\Foundation\Auth\User as Authenticatable; Class Admin extends Authenticatable { // } 

2: Update config/auth.php.

return [ // This is the default guard used, not need to declare // another guard here 'defaults' => [ 'guard' => 'user', 'passwords' => 'user', ], // Here we must to declare the guards, if we created the App\Admin // class as first step, we don't need to create a custom guard 'guards' => [ 'user' => [ 'driver' => 'session', 'provider' => 'user', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admin', ], ], // In this example we are using only 'eloquent' driver 'providers' => [ 'user' => [ 'driver' => 'eloquent', 'model' => 'App\User', ], 'admin' => [ 'driver' => 'eloquent', 'model' => 'App\Admin', ], ], 'passwords' => [ 'user' => [ 'provider' => 'user', 'email' => 'auth.emails.password', 'table' => 'password_resets', 'expire' => 60, ], 'admin' => [ 'provider' => 'admin', 'email' => 'auth.emails.password', 'table' => 'password_resets', 'expire' => 60, ] ] ]; 

3: To test it, we can use our app\Http\Route.php file:

Route::get('/login', function() { $auth = auth()->guard('admin'); $credentials = [ 'email' => '[email protected]', 'password' => 'password', ]; if ($auth->attempt($credentials)) { return 'Success'; } else { return 'Not Success'; }); 

Comments

0

Thanks for answer HoLiC, it's working right now but can you try to implement it with classes what Laravel's bring on start? You just have to add routes:

Route::controller('/auth', 'Auth\AuthController'); Route::controller('/password', 'Auth\PasswordController'); 

and create form in resources/views/auth/login.blade.php from my post above. After this you can use routes laravel.dev/auth/login and laravel.dev/auth/logout

Starter auth mechanism doesn't working good and its not compatibile with multi auth. If you check you can login via laravel.dev/auth/login but only user(theres no any way to setup in AuthController or anyplace to use admins) and logout action not working. If you check this trait Illuminate\Foundation\Auth\AuthenticatesUsers you will see that this mechanims is unuseful right now for example logout method not defines anywhere provider admin:

Auth::logout(); // not working logout should be smth like Auth::guard($provider)->logout(); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.