Skip to content
Draft
Show file tree
Hide file tree
Changes from 5 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
32 changes: 32 additions & 0 deletions app/GraphQL/Mutations/Login.php
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'));
Comment thread
spawnia marked this conversation as resolved.
Outdated

if( ! $guard->attempt($args)) {
Comment thread
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;
}
}
24 changes: 24 additions & 0 deletions app/GraphQL/Mutations/Logout.php
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;
}
}
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 @@ 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,
],
Comment thread
spawnia marked this conversation as resolved.
Outdated
'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
72 changes: 66 additions & 6 deletions 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
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',
Comment thread
spawnia marked this conversation as resolved.
Outdated
'provider' => 'users',
],

'api' => [
'driver' => 'token',
'provider' => 'users',
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),
];
4 changes: 3 additions & 1 deletion 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,
Comment thread
spawnia marked this conversation as resolved.
Outdated

// 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 All @@ -54,7 +56,7 @@
|
*/

'guard' => 'api',
'guard' => 'sanctum',

/*
|--------------------------------------------------------------------------
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')),
Comment thread
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,
],

];
Loading