Created
July 20, 2022 09:15
-
-
Save amsoell/cee8fb79c2790a98f060bdd16a666e4b to your computer and use it in GitHub Desktop.
Extend Laravel Sanctum
This file contains hidden or 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
diff --git a/app/Guards/ChordGuard.php b/app/Guards/ChordGuard.php | |
new file mode 100644 | |
index 0000000..6b5ef74 | |
--- /dev/null | |
+++ b/web/app/Guards/ChordGuard.php | |
@@ -0,0 +1,18 @@ | |
+<?php | |
+ | |
+namespace App\Guards; | |
+ | |
+use Illuminate\Http\Request; | |
+use Laravel\Sanctum\Guard; | |
+ | |
+class ChordGuard extends Guard | |
+{ | |
+ public function __invoke(Request $request) | |
+ { | |
+ if ($tokenable = parent::__invoke($request)) { | |
+ return $tokenable; | |
+ } | |
+ | |
+ // If token is invalid, custom logic here | |
+ } | |
+} | |
diff --git a/web/app/Providers/AppServiceProvider.php b/web/app/Providers/AppServiceProvider.php | |
index 6cc5687..5bf5c51 100644 | |
--- a/app/Providers/AppServiceProvider.php | |
+++ b/app/Providers/AppServiceProvider.php | |
@@ -2,9 +2,12 @@ | |
namespace App\Providers; | |
+use App\Guards\ChordGuard; | |
use Contentful\Management\Client as ContentfulClient; | |
use Contentful\Management\Proxy\EnvironmentProxy; | |
+use Illuminate\Auth\RequestGuard; | |
use Illuminate\Support\Arr; | |
+use Illuminate\Support\Facades\Auth; | |
use Illuminate\Support\ServiceProvider; | |
class AppServiceProvider extends ServiceProvider | |
@@ -44,5 +47,22 @@ public function boot() | |
return $array; | |
}); | |
+ | |
+ Auth::resolved(function ($auth) { | |
+ $auth->extend('chord', function ($app, $name, array $config) use ($auth) { | |
+ return tap($this->createGuard($auth, $config), function ($guard) { | |
+ $this->app->refresh('request', $guard, 'setRequest'); | |
+ }); | |
+ }); | |
+ }); | |
+ } | |
+ | |
+ protected function createGuard($auth, $config) | |
+ { | |
+ return new RequestGuard( | |
+ new ChordGuard($auth, config('sanctum.expiration'), $config['provider']), | |
+ $this->app['request'], | |
+ $auth->createUserProvider() | |
+ ); | |
} | |
} | |
diff --git a/web/config/auth.php b/web/config/auth.php | |
index 54b3c51..0f805cd 100644 | |
--- a/config/auth.php | |
+++ b/config/auth.php | |
@@ -40,6 +40,11 @@ | |
'driver' => 'session', | |
'provider' => 'users', | |
], | |
+ | |
+ 'chord' => [ | |
+ 'driver' => 'chord', | |
+ 'provider' => null, | |
+ ], | |
], | |
/* | |
diff --git a/web/config/lighthouse.php b/web/config/lighthouse.php | |
index 9fafdf5..994588c 100644 | |
--- a/web/config/lighthouse.php | |
+++ b/web/config/lighthouse.php | |
@@ -57,7 +57,7 @@ | |
| | |
*/ | |
- 'guard' => 'sanctum', | |
+ 'guard' => 'chord', | |
/* | |
|-------------------------------------------------------------------------- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment