Created
March 8, 2023 09:16
-
-
Save mtvbrianking/b840800a50ba5dce34558b036b114efe to your computer and use it in GitHub Desktop.
Laravel MultiAuth - Guard & User Provider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Modules\OneTwos\Auth; | |
use Illuminate\Auth\SessionGuard; | |
class OneTwoGuard extends SessionGuard | |
{ | |
// ... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Modules\OneTwos; | |
use App\Modules\OneTwos\Auth\OneTwoGuard; | |
use App\Modules\OneTwos\Auth\OneTwoUserProvider; | |
use App\Modules\OneTwos\Models\OneTwo; | |
use Illuminate\Support\Facades\Auth; | |
use Illuminate\Support\ServiceProvider; | |
class OneTwoServiceProvider extends ServiceProvider | |
{ | |
/** | |
* Bootstrap the application services. | |
*/ | |
public function boot(): void | |
{ | |
// ... | |
} | |
/** | |
* Register the application services. | |
*/ | |
public function register(): void | |
{ | |
$this->injectAuthConfiguration(); | |
$this->registerAuthDrivers('one-twos', 'one-two', OneTwo::class); | |
} | |
/** | |
* @see \Illuminate\Auth\AuthManager | |
* @see https://www.devrohit.com/custom-authentication-in-laravel | |
*/ | |
protected function registerAuthDrivers(string $provider, string $guard, string $model) | |
{ | |
Auth::provider('one_two_provider_driver', function ($app) use ($model) { | |
return new OneTwoUserProvider($app['hash'], $model); | |
}); | |
/* AuthManager->createSessionDriver() */ | |
Auth::extend('one_two_guard_driver', function ($app) use ($provider, $guard) { | |
$userProvider = Auth::createUserProvider($provider); | |
$oneTwoGuard = new OneTwoGuard($guard, $userProvider, $app['session.store']); | |
if (method_exists($oneTwoGuard, 'setCookieJar')) { | |
$oneTwoGuard->setCookieJar($this->app['cookie']); | |
} | |
if (method_exists($oneTwoGuard, 'setDispatcher')) { | |
$oneTwoGuard->setDispatcher($this->app['events']); | |
} | |
if (method_exists($oneTwoGuard, 'setRequest')) { | |
$oneTwoGuard->setRequest($this->app->refresh('request', $oneTwoGuard, 'setRequest')); | |
} | |
if (isset($config['remember'])) { | |
$oneTwoGuard->setRememberDuration($config['remember']); | |
} | |
return $oneTwoGuard; | |
}); | |
} | |
protected function injectAuthConfiguration() | |
{ | |
$this->app['config']->set('auth.guards.one-two', [ | |
// 'driver' => 'session', | |
'driver' => 'one_two_guard_driver', | |
'provider' => 'one-twos', | |
]); | |
$this->app['config']->set('auth.providers.one-twos', [ | |
// 'driver' => 'eloquent', | |
'driver' => 'one_two_provider_driver', | |
'model' => Models\OneTwo::class, | |
]); | |
$this->app['config']->set('auth.passwords.one-twos', [ | |
'provider' => 'one-twos', | |
'table' => 'one_two_password_reset_tokens', | |
'expire' => 60, | |
'throttle' => 60, | |
]); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Modules\OneTwos\Auth; | |
use Illuminate\Auth\EloquentUserProvider; | |
class OneTwoUserProvider extends EloquentUserProvider | |
{ | |
// ... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment