Skip to content
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

Implement session based authentication with Sanctum #9

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions app/GraphQL/Mutations/Login.php
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)) {
spawnia marked this conversation as resolved.
Show resolved Hide resolved
throw new Error('Invalid credentials.');
}

$user = $guard->user();
assert($user instanceof User, 'must receive User after successful login');

return $user;
}
}
27 changes: 27 additions & 0 deletions app/GraphQL/Mutations/Logout.php
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;
}
}
6 changes: 6 additions & 0 deletions app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ final class Kernel extends HttpKernel
* @var array<string, array<string|class-string>>
*/
protected $middlewareGroups = [
'web' => [
\Illuminate\Cookie\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\Foundation\Http\Middleware\VerifyCsrfToken::class,
],
spawnia marked this conversation as resolved.
Show resolved Hide resolved
'api' => [
'throttle:60,1',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"php": "^8.0.2",
"guzzlehttp/guzzle": "^7.2",
"laravel/framework": "^9.11",
"laravel/sanctum": "^2.15",
"laravel/tinker": "^2.7",
"mll-lab/laravel-graphql-playground": "^2",
"nuwave/lighthouse": "^5"
Expand Down
67 changes: 66 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@
Illuminate\Queue\QueueServiceProvider::class,
Illuminate\Redis\RedisServiceProvider::class,
Illuminate\Auth\Passwords\PasswordResetServiceProvider::class,
Illuminate\Session\SessionServiceProvider::class,
Illuminate\Translation\TranslationServiceProvider::class,
Illuminate\Validation\ValidationServiceProvider::class,
Illuminate\View\ViewServiceProvider::class,
Expand Down Expand Up @@ -214,6 +215,7 @@
'Response' => Illuminate\Support\Facades\Response::class,
'Route' => Illuminate\Support\Facades\Route::class,
'Schema' => Illuminate\Support\Facades\Schema::class,
'Session' => Illuminate\Support\Facades\Session::class,
'Storage' => Illuminate\Support\Facades\Storage::class,
'Str' => Illuminate\Support\Str::class,
'URL' => Illuminate\Support\Facades\URL::class,
Expand Down
5 changes: 5 additions & 0 deletions config/auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
*/

'guards' => [
'web' => [
'driver' => 'session',
spawnia marked this conversation as resolved.
Show resolved Hide resolved
'provider' => 'users',
],

'api' => [
'driver' => 'token',
'provider' => 'users',
Expand Down
63 changes: 63 additions & 0 deletions config/graphql-playground.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?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',

/*
|--------------------------------------------------------------------------
| Subscription endpoint
|--------------------------------------------------------------------------
|
| The default subscription endpoint Playground 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`
|
*/

'subscriptionEndpoint' => env('GRAPHQL_PLAYGROUND_SUBSCRIPTION_ENDPOINT', null),

/*
|--------------------------------------------------------------------------
| 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),
];
Loading