Laravel 5.2 needs an example that implements the default / "Multi-Auth" authentication drivers. which requires a lot of work right now because

Authentication Drivers / "Multi-Auth"

as before the release of laravel 5.2, it is claimed that multi-user calipers are out of the box. but there are no sample codes showing how to authenticate using different drivers using routes. So I need help setting up multi-auth using larvel by default 5.2

+4
authentication php laravel
Dec 22 '15 at 10:40
source share
4 answers

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 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 install below code and test

  Route::get('/login', function() { $auth = auth()->guard('admin'); $credentials = [ 'email' => 'admin@gmail.com', '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()); } }); 
+6
Dec 22 '15 at 12:49
source share

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

1: Create the App \ Admin model (in our application folder). This is how your Authenticatable class should be.

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

2: update the configuration /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 this, we can use our application file \ Http \ Route.php:

 Route::get('/login', function() { $auth = auth()->guard('admin'); $credentials = [ 'email' => 'admin@gmail.com', 'password' => 'password', ]; if ($auth->attempt($credentials)) { return 'Success'; } else { return 'Not Success'; }); 
+1
Dec 30 '15 at 6:57
source share

Thanks for the HoLiC answer, it works right now, but can you try to implement it with the classes that Laravel launches? You just need to add routes:

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

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

The starter start mechanism does not work and is not compatible with multi auth. If you check, you can log in via laravel.dev/auth/login, but only the user (there is no way to configure in AuthController or anywhere for administrators to use) and the logout action does not work. If you check this sign Illuminate \ Foundation \ Auth \ AuthenticatesUsers, you will see that this mechanic is not used now, for example, the logout method does not determine the provider administrator anywhere:

 Auth::logout(); // not working logout should be smth like Auth::guard($provider)->logout(); 
-one
Dec 30 '15 at 9:47
source share

bartw2k9, to resolve the default Auth user, check the Client Configuration in the docs. Setting the property protected $guard = 'admin'; AuthController indicated by Laravel, which uses default authentication. https://laravel.com/docs/master/authentication#authentication-quickstart

-one
Jan 11 '16 at 21:34
source share



All Articles