mirror of
https://github.com/bitinflow/accounts.git
synced 2026-03-15 06:25:52 +00:00
Add jwt handling, inspired by passport
This commit is contained in:
@@ -4,7 +4,12 @@ declare(strict_types=1);
|
||||
|
||||
namespace GhostZero\BitinflowAccounts\Providers;
|
||||
|
||||
use GhostZero\BitinflowAccounts\Auth\TokenGuard;
|
||||
use GhostZero\BitinflowAccounts\Auth\UserProvider;
|
||||
use GhostZero\BitinflowAccounts\BitinflowAccounts;
|
||||
use GhostZero\BitinflowAccounts\Helpers\JwtParser;
|
||||
use Illuminate\Auth\RequestGuard;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class BitinflowAccountsServiceProvider extends ServiceProvider
|
||||
@@ -33,6 +38,8 @@ class BitinflowAccountsServiceProvider extends ServiceProvider
|
||||
$this->app->singleton(BitinflowAccounts::class, function () {
|
||||
return new BitinflowAccounts;
|
||||
});
|
||||
|
||||
$this->registerGuard();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -43,4 +50,37 @@ class BitinflowAccountsServiceProvider extends ServiceProvider
|
||||
{
|
||||
return [BitinflowAccounts::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the token guard.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function registerGuard()
|
||||
{
|
||||
Auth::resolved(function ($auth) {
|
||||
$auth->extend('bitinflow-accounts', function ($app, $name, array $config) {
|
||||
return tap($this->makeGuard($config), function ($guard) {
|
||||
$this->app->refresh('request', $guard, 'setRequest');
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Make an instance of the token guard.
|
||||
*
|
||||
* @param array $config
|
||||
* @return RequestGuard
|
||||
*/
|
||||
protected function makeGuard(array $config): RequestGuard
|
||||
{
|
||||
return new RequestGuard(function ($request) use ($config) {
|
||||
return (new TokenGuard(
|
||||
new UserProvider(Auth::createUserProvider($config['provider']), $config['provider']),
|
||||
$this->app->make('encrypter'),
|
||||
$this->app->make(JwtParser::class)
|
||||
))->user($request);
|
||||
}, $this->app['request']);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user