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 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;
}
}
2 changes: 1 addition & 1 deletion app/Models/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
*/
final class Comment extends Model
{
/** @return \Illuminate\Database\Eloquent\Relations\BelongsTo<\App\Models\Post, self> */
/** @return \Illuminate\Database\Eloquent\Relations\BelongsTo<\App\Models\Post, $this> */
public function post(): BelongsTo
{
return $this->belongsTo(Post::class);
Expand Down
4 changes: 2 additions & 2 deletions app/Models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ protected static function booted(): void
});
}

/** @return \Illuminate\Database\Eloquent\Relations\BelongsTo<\App\Models\User, self> */
/** @return \Illuminate\Database\Eloquent\Relations\BelongsTo<\App\Models\User, $this> */
public function author(): BelongsTo
{
return $this->belongsTo(User::class);
}

/** @return \Illuminate\Database\Eloquent\Relations\HasMany<\App\Models\Comment> */
/** @return \Illuminate\Database\Eloquent\Relations\HasMany<\App\Models\Comment, $this> */
public function comments(): HasMany
{
return $this->hasMany(Comment::class);
Expand Down
2 changes: 1 addition & 1 deletion app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ final class User extends Authenticatable
'email_verified_at' => 'datetime',
];

/** @return \Illuminate\Database\Eloquent\Relations\HasMany<\App\Models\Post> */
/** @return \Illuminate\Database\Eloquent\Relations\HasMany<\App\Models\Post, $this> */
public function posts(): HasMany
{
return $this->hasMany(Post::class, 'author_id');
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"require": {
"php": "^8.2",
"laravel/framework": "^11",
"laravel/sanctum": "^4",
"laravel/tinker": "^2.9",
"mll-lab/laravel-graphiql": "^3",
"nuwave/lighthouse": "^6"
Expand Down
Loading
Loading