Laravel 5.2 need an example which implements default Authentication Drivers / "Multi-Auth". which needs lots of works right now as
Solution 1
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());
}
});
Solution 2
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';
});
msonowal
Snr. Software Developer developer @clarity. I primarily work in PHP and JS and contribute to the open-source projects. A avid gamer likes to play COD and Dirt. I love the Laravel PHP framework and Vue JS.
Updated on June 04, 2022Comments
-
msonowal almost 2 years
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