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 9 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
25 changes: 25 additions & 0 deletions app/GraphQL/Mutations/Login.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?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();
spawnia marked this conversation as resolved.
Show resolved Hide resolved

if( ! $guard->attempt($args)) {
spawnia marked this conversation as resolved.
Show resolved Hide resolved
throw new Error('Invalid credentials.');
}

return $guard->user();
}
}
23 changes: 23 additions & 0 deletions app/GraphQL/Mutations/Logout.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?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();

$user = $guard->user();
$guard->logout();

return $user;
}
}
5 changes: 5 additions & 0 deletions app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ class Kernel extends HttpKernel
* @var array<string, array<string|class-string>>
*/
protected $middlewareGroups = [
'web' => [
\Illuminate\Cookie\Middleware\EncryptCookies::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 @@ -15,6 +15,7 @@
"fruitcake/laravel-cors": "^1.0",
"guzzlehttp/guzzle": "^6.3",
"laravel/framework": "^7.0",
"laravel/sanctum": "^2.4",
"mll-lab/laravel-graphql-playground": "^2.1",
"nuwave/lighthouse": "^4.15.0"
},
Expand Down
62 changes: 61 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 @@ -217,6 +218,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
2 changes: 1 addition & 1 deletion config/auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

'guards' => [
'api' => [
'driver' => 'token',
'driver' => 'session',
spawnia marked this conversation as resolved.
Show resolved Hide resolved
'provider' => 'users',
'hash' => false,
],
Expand Down
49 changes: 49 additions & 0 deletions config/graphql-playground.php
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),
];
2 changes: 2 additions & 0 deletions config/lighthouse.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
'middleware' => [
\Nuwave\Lighthouse\Support\Http\Middleware\AcceptJson::class,

\Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
spawnia marked this conversation as resolved.
Show resolved Hide resolved

// 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\Support\Http\Middleware\AttemptAuthentication::class,
Expand Down
47 changes: 47 additions & 0 deletions config/sanctum.php
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,
],

];
Loading