Skip to content

Multiple Guards

Jim edited this page Apr 12, 2019 · 4 revisions

FlightDeck makes it easy to add authentication and password reset functionality across multiple guards.

For example, suppose your application has a default authentication guard for "pilots". You would like to add a separate guard for "crew".

First you would follow the usual process of updating the config/auth.php file in order to add crew to the guards, providers and passwords arrays:

'guards' => [
    'crew' => [
        'driver' => 'jwt',
        'provider' => 'crew',
    ],
],
'providers' => [
    'crew' => [
        'driver' => 'eloquent',
        'model' => App\Models\Crew::class,
    ],
],
'passwords' => [
    'crew' => [
        'provider' => 'crew',
        'table' => 'crew_password_resets',
        'expire' => 60,
    ],
],

Next, you can simply extend the provided AuthController, ForgotPasswordController and ResetPasswordController in order to specify which guard and password broker to use:

App\Http\Controllers\Crew\AuthController.php

namespace App\Http\Controllers\Crew;

use Yab\FlightDeck\Http\Controllers\AuthController as FlightAuthController;

class AuthController extends FlightAuthController
{
    protected function guard()
    {
        return auth()->guard('crew');
    }
}

App\Http\Controllers\Crew\ForgotPasswordController.php

namespace App\Http\Controllers\Crew;

use Illuminate\Support\Facades\Password;
use Yab\FlightDeck\Http\Controllers\ForgotPasswordController as FlightDeckForgotPasswordController;

class ForgotPasswordController extends FlightDeckForgotPasswordController
{
    public function broker()
    {
        return Password::broker('crew');
    }
}

App\Http\Controllers\Crew\ResetPasswordController.php

namespace App\Http\Controllers\Crew;

use Illuminate\Support\Facades\Password;
use Yab\FlightDeck\Http\Controllers\ResetPasswordController as FlightDeckResetPasswordController;

class ResetPasswordController extends FlightDeckResetPasswordController
{
    protected function guard()
    {
        return auth()->guard('crew');
    }

    public function broker()
    {
        return Password::broker('crew');
    }
}

Finally, point to your new controllers from your routes file:

Route::group([
	'prefix' => 'crew',
	'namespace' => '\App\Http\Controllers\Crew',
], function () {
    Route::post('login', 'AuthController@store')->name('crew.login');
    Route::post('logout', 'AuthController@destroy')->name('crew.logout');
    Route::post('refresh', 'AuthController@update')->name('crew.refresh');
    Route::post('password/email', 'ForgotPasswordController@sendResetEmail')->name('crew.password.email');
    Route::post('password/reset', 'ResetPasswordController@reset')->name('crew.password.reset');
});