-
Notifications
You must be signed in to change notification settings - Fork 16
Implement session based authentication with Sanctum #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
spawnia
wants to merge
17
commits into
master
Choose a base branch
from
sanctum
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
2e7ade3
Implement session based authentication with Sanctum
spawnia 915a4a5
Incorporate feedback, fix static analysis
spawnia dffc447
composer update
spawnia 9249abd
Merge branch 'master' into sanctum
spawnia 8170cb1
Add missing web guard back in
spawnia ec6b639
Merge branch 'master' into sanctum
spawnia 838fa7a
Update Sanctum with Laravel 9
spawnia 871aa29
Merge branch 'master' into sanctum
spawnia 97d6bc1
Laravel 11
spawnia c28e85c
Delete unnecessary config
spawnia 25d079a
add return type
spawnia 545d294
composer update
spawnia 4edb121
seed
spawnia 50228b2
Merge branch 'master' into sanctum
spawnia 875d4db
composer update
spawnia 9f76232
describe login
spawnia 55b117e
Prettify docs
spawnia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| <?php | ||
|
|
||
| namespace App\GraphQL\Mutations; | ||
|
|
||
| use App\Models\User; | ||
| use GraphQL\Error\Error; | ||
| use Illuminate\Support\Arr; | ||
| use Illuminate\Support\Facades\Auth; | ||
|
|
||
| final class Login | ||
| { | ||
| /** | ||
| * @param null $_ | ||
| * @param array{email: string, password: string} $args | ||
| */ | ||
| public function __invoke($_, array $args): User | ||
| { | ||
| $guardConfig = config('sanctum.guard'); | ||
| assert(is_array($guardConfig)); | ||
|
|
||
| $guardName = Arr::first($guardConfig); | ||
| assert(is_string($guardName)); | ||
|
|
||
| $guard = Auth::guard($guardName); | ||
|
|
||
| if( ! $guard->attempt($args)) { | ||
| throw new Error('Invalid credentials.'); | ||
| } | ||
|
|
||
| $user = $guard->user(); | ||
| assert($user instanceof User, 'must receive User after successful login'); | ||
|
|
||
| return $user; | ||
| } | ||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| <?php | ||
|
|
||
| namespace App\GraphQL\Mutations; | ||
|
|
||
| use App\Models\User; | ||
| use Illuminate\Support\Arr; | ||
| use Illuminate\Support\Facades\Auth; | ||
|
|
||
| final class Logout | ||
| { | ||
| public function __invoke(): ?User | ||
| { | ||
| $guardConfig = config('sanctum.guard'); | ||
| assert(is_array($guardConfig)); | ||
|
|
||
| $guardName = Arr::first($guardConfig); | ||
| assert(is_string($guardName)); | ||
|
|
||
| $guard = Auth::guard($guardName); | ||
|
|
||
| $user = $guard->user(); | ||
|
|
||
| $guard->logout(); | ||
|
|
||
| return $user; | ||
| } | ||
| } |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| <?php declare(strict_types=1); | ||
|
|
||
| return [ | ||
| /* | ||
| |-------------------------------------------------------------------------- | ||
| | Routes configuration | ||
| |-------------------------------------------------------------------------- | ||
| | | ||
| | Set the key as URI at which the GraphiQL UI can be viewed, | ||
| | and add any additional configuration for the route. | ||
| | | ||
| | You can add multiple routes pointing to different GraphQL endpoints. | ||
| | | ||
| */ | ||
|
|
||
| 'routes' => [ | ||
| '/graphiql' => [ | ||
| 'name' => 'graphiql', | ||
| 'middleware' => ['web'], | ||
| // 'prefix' => '', | ||
| // 'domain' => 'graphql.' . env('APP_DOMAIN', 'localhost'), | ||
|
|
||
| /* | ||
| |-------------------------------------------------------------------------- | ||
| | Default GraphQL endpoint | ||
| |-------------------------------------------------------------------------- | ||
| | | ||
| | The default endpoint that the GraphiQL UI is set to. | ||
| | It assumes you are running GraphQL on the same domain | ||
| | as GraphiQL, but can be set to any URL. | ||
| | | ||
| */ | ||
|
|
||
| 'endpoint' => '/graphql', | ||
|
|
||
| /* | ||
| |-------------------------------------------------------------------------- | ||
| | Subscription endpoint | ||
| |-------------------------------------------------------------------------- | ||
| | | ||
| | The default subscription endpoint the GraphiQL UI uses to connect to. | ||
| | Tries to connect to the `endpoint` value if `null` as ws://{{endpoint}} | ||
| | | ||
| | Example: `ws://your-endpoint` or `wss://your-endpoint` | ||
| | | ||
| */ | ||
|
|
||
| 'subscription-endpoint' => env('GRAPHIQL_SUBSCRIPTION_ENDPOINT', null), | ||
| ], | ||
| ], | ||
|
|
||
| /* | ||
| |-------------------------------------------------------------------------- | ||
| | Control GraphiQL availability | ||
| |-------------------------------------------------------------------------- | ||
| | | ||
| | Control if the GraphiQL UI is accessible at all. | ||
| | This allows you to disable it in certain environments, | ||
| | for example you might not want it active in production. | ||
| | | ||
| */ | ||
|
|
||
| 'enabled' => env('GRAPHIQL_ENABLED', true), | ||
| ]; |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| <?php declare(strict_types=1); | ||
|
|
||
| return [ | ||
| /* | ||
| |-------------------------------------------------------------------------- | ||
| | Route Configuration | ||
| |-------------------------------------------------------------------------- | ||
| | | ||
| | Controls the HTTP route that your GraphQL server responds to. | ||
| | You may set `route` => false, to disable the default route | ||
| | registration and take full control. | ||
| | | ||
| */ | ||
|
|
||
| 'route' => [ | ||
| /* | ||
| * The URI the endpoint responds to, e.g. mydomain.com/graphql. | ||
| */ | ||
| 'uri' => '/graphql', | ||
|
|
||
| /* | ||
| * Lighthouse creates a named route for convenient URL generation and redirects. | ||
| */ | ||
| 'name' => 'graphql', | ||
|
|
||
| /* | ||
| * Beware that middleware defined here runs before the GraphQL execution phase, | ||
| * make sure to return spec-compliant responses in case an error is thrown. | ||
| */ | ||
| 'middleware' => [ | ||
| // Ensures the request is not vulnerable to cross-site request forgery. | ||
| // Nuwave\Lighthouse\Http\Middleware\EnsureXHR::class, | ||
|
|
||
| // Always set the `Accept: application/json` header. | ||
| Nuwave\Lighthouse\Http\Middleware\AcceptJson::class, | ||
|
|
||
| Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class, | ||
|
|
||
| // Logs in a user if they are authenticated. In contrast to Laravel's 'auth' | ||
| // middleware, this delegates auth and permission checks to the field level. | ||
| Nuwave\Lighthouse\Http\Middleware\AttemptAuthentication::class, | ||
|
|
||
| // Logs every incoming GraphQL query. | ||
| // Nuwave\Lighthouse\Http\Middleware\LogGraphQLQueries::class, | ||
| ], | ||
|
|
||
| /* | ||
| * The `prefix`, `domain` and `where` configuration options are optional. | ||
| */ | ||
| // 'prefix' => '', | ||
| // 'domain' => '', | ||
| // 'where' => [], | ||
| ], | ||
| ]; |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| <?php | ||
|
|
||
| return [ | ||
|
|
||
| /* | ||
| |-------------------------------------------------------------------------- | ||
| | Stateful Domains | ||
| |-------------------------------------------------------------------------- | ||
| | | ||
| | Requests from the following domains / hosts will receive stateful API | ||
| | authentication cookies. Typically, these should include your local | ||
| | and production domains which access your API via a frontend SPA. | ||
| | | ||
| */ | ||
|
|
||
| 'stateful' => explode(',', env('SANCTUM_STATEFUL_DOMAINS', 'localhost,127.0.0.1,127.0.0.1:8000,::1')), | ||
spawnia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /* | ||
| |-------------------------------------------------------------------------- | ||
| | Expiration Minutes | ||
| |-------------------------------------------------------------------------- | ||
| | | ||
| | This value controls the number of minutes until an issued token will be | ||
| | considered expired. If this value is null, personal access tokens do | ||
| | not expire. This won't tweak the lifetime of first-party sessions. | ||
| | | ||
| */ | ||
|
|
||
| 'expiration' => null, | ||
|
|
||
| /* | ||
| |-------------------------------------------------------------------------- | ||
| | Sanctum Middleware | ||
| |-------------------------------------------------------------------------- | ||
| | | ||
| | When authenticating your first-party SPA with Sanctum you may need to | ||
| | customize some of the middleware Sanctum uses while processing the | ||
| | request. You may change the middleware listed below as required. | ||
| | | ||
| */ | ||
|
|
||
| 'middleware' => [ | ||
| 'verify_csrf_token' => \Illuminate\Foundation\Http\Middleware\VerifyCsrfToken::class, | ||
| 'encrypt_cookies' => \Illuminate\Cookie\Middleware\EncryptCookies::class, | ||
| ], | ||
|
|
||
| ]; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.