-
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 5 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| <?php | ||
|
|
||
| namespace App\GraphQL\Mutations; | ||
|
|
||
| use App\Models\User; | ||
| use GraphQL\Error\Error; | ||
| use Illuminate\Support\Facades\Auth; | ||
|
|
||
| class Login | ||
| { | ||
| /** | ||
| * @param null $_ | ||
| * @param array<string, mixed> $args | ||
| */ | ||
| public function __invoke($_, array $args): User | ||
| { | ||
| $guard = Auth::guard(config('sanctum.guard', 'web')); | ||
|
|
||
| if( ! $guard->attempt($args)) { | ||
|
spawnia marked this conversation as resolved.
|
||
| throw new Error('Invalid credentials.'); | ||
| } | ||
|
|
||
| /** | ||
| * Since we successfully logged in, this can no longer be `null`. | ||
| * | ||
| * @var \App\Models\User $user | ||
| */ | ||
| $user = $guard->user(); | ||
|
|
||
| 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,24 @@ | ||
| <?php | ||
|
|
||
| namespace App\GraphQL\Mutations; | ||
|
|
||
| use App\Models\User; | ||
| use Illuminate\Support\Facades\Auth; | ||
|
|
||
| class Logout | ||
| { | ||
| /** | ||
| * @param null $_ | ||
| * @param array<string, mixed> $args | ||
| */ | ||
| public function __invoke($_, array $args): ?User | ||
| { | ||
| $guard = Auth::guard(config('sanctum.guard', 'web')); | ||
|
|
||
| /** @var \App\Models\User|null $user */ | ||
| $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
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
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,49 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| return [ | ||
| /* | ||
| |-------------------------------------------------------------------------- | ||
| | Route configuration | ||
| |-------------------------------------------------------------------------- | ||
| | | ||
| | Set the URI at which the GraphQL Playground can be viewed | ||
| | and any additional configuration for the route. | ||
| | | ||
| */ | ||
|
|
||
| 'route' => [ | ||
| 'uri' => '/graphql-playground', | ||
| 'name' => 'graphql-playground', | ||
| 'middleware' => ['web'] | ||
| // 'prefix' => '', | ||
| // 'domain' => 'graphql.' . env('APP_DOMAIN', 'localhost'), | ||
| ], | ||
|
|
||
| /* | ||
| |-------------------------------------------------------------------------- | ||
| | Default GraphQL endpoint | ||
| |-------------------------------------------------------------------------- | ||
| | | ||
| | The default endpoint that the Playground UI is set to. | ||
| | It assumes you are running GraphQL on the same domain | ||
| | as GraphQL Playground, but can be set to any URL. | ||
| | | ||
| */ | ||
|
|
||
| 'endpoint' => '/graphql', | ||
|
|
||
| /* | ||
| |-------------------------------------------------------------------------- | ||
| | Control Playground availability | ||
| |-------------------------------------------------------------------------- | ||
| | | ||
| | Control if the playground 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('GRAPHQL_PLAYGROUND_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
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.
|
||
|
|
||
| /* | ||
| |-------------------------------------------------------------------------- | ||
| | 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.