Skip to content

Commit

Permalink
Finished JWT auth implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
WendellAdriel committed Apr 27, 2024
1 parent ca5a692 commit 8a99a4c
Show file tree
Hide file tree
Showing 17 changed files with 66 additions and 28 deletions.
2 changes: 1 addition & 1 deletion app/Providers/RouteServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ private function configureIndexRoute(): void
'datetime' => Carbon::now()->format(Formatter::API_DATETIME_FORMAT),
];

if (! App::environment('local')) {
if (! App::environment('local', 'testing')) {
return response()->json($data);
}

Expand Down
7 changes: 6 additions & 1 deletion config/auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/

'defaults' => [
'guard' => 'web',
'guard' => 'api',
'passwords' => 'users',
],

Expand All @@ -42,6 +42,11 @@
'driver' => 'session',
'provider' => 'users',
],

'api' => [
'driver' => 'jwt',
'provider' => 'users',
],
],

/*
Expand Down
5 changes: 5 additions & 0 deletions config/cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@
'driver' => 'octane',
],

'jwt' => [
'driver' => 'redis',
'connection' => 'jwt',
'lock_connection' => 'default',
],
],

/*
Expand Down
9 changes: 9 additions & 0 deletions config/database.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,15 @@
'database' => env('REDIS_CACHE_DB', '1'),
],

'jwt' => [
'url' => env('REDIS_URL'),
'host' => env('REDIS_HOST', '127.0.0.1'),
'username' => env('REDIS_USERNAME'),
'password' => env('REDIS_PASSWORD'),
'port' => env('REDIS_PORT', '6379'),
'database' => env('JWT_DB', '2'),
],

],

];
4 changes: 3 additions & 1 deletion config/jwt.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/*
* This file is part of jwt-auth.
*
Expand Down Expand Up @@ -294,7 +296,7 @@
|
*/

'storage' => Tymon\JWTAuth\Providers\Storage\Illuminate::class,
'storage' => \Exa\Support\JWTStorage::class,

],

Expand Down
2 changes: 1 addition & 1 deletion exa/DTOs/DatatableDTO.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ protected function defaults(): array
'page' => 1,
'per_page' => 20,
'sort_field' => null,
'sort_order' => null,
'sort_order' => SortOption::ASC,
'search' => null,
];
}
Expand Down
2 changes: 1 addition & 1 deletion exa/Http/Middlewares/BlockViewerUsers.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

final class BlockViewerUsers
{
public function handle(Request $request, Closure $next): Closure
public function handle(Request $request, Closure $next): mixed
{
$user = Auth::user();
if (is_null($user)) {
Expand Down
2 changes: 1 addition & 1 deletion exa/Http/Middlewares/HasRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function __construct(private HasRoleAction $action)
/**
* @throws AccessDeniedException
*/
public function handle(Request $request, Closure $next, string $role): Closure
public function handle(Request $request, Closure $next, string $role): mixed
{
$user = Auth::user();
if (is_null($user)) {
Expand Down
18 changes: 18 additions & 0 deletions exa/Support/JWTStorage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Exa\Support;

use Illuminate\Contracts\Cache\Repository as CacheContract;
use Illuminate\Support\Facades\Cache;
use Tymon\JWTAuth\Providers\Storage\Illuminate;

final class JWTStorage extends Illuminate
{
public function __construct(CacheContract $cache)
{
parent::__construct($cache);
$this->cache = Cache::store('jwt');
}
}
6 changes: 3 additions & 3 deletions modules/Auth/Actions/Login.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Illuminate\Auth\AuthenticationException;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;
use Modules\Auth\DTOs\LoginDTO;
use Modules\Auth\Models\User;
use Modules\Auth\Models\UserLogin;
Expand All @@ -17,7 +16,8 @@

public function handle(LoginDTO $dto): array
{
if (! Auth::attempt($dto->toArray())) {
$token = Auth::attempt($dto->toArray());
if (! $token) {
throw new AuthenticationException();
}

Expand All @@ -27,7 +27,7 @@ public function handle(LoginDTO $dto): array

return [
'type' => self::TOKEN_TYPE,
'token' => $user->createToken(Str::slug(config('app.name') . '_login'))->plainTextToken,
'token' => $token,
];
}

Expand Down
2 changes: 1 addition & 1 deletion modules/Auth/Controllers/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function login(Request $request, Login $action): ApiSuccessResponse
#[OA\Response(response: '500', description: 'Server Error')]
public function logout(): NoContentResponse
{
Auth::guard('web')->logout();
auth()->logout(true);

return new NoContentResponse();
}
Expand Down
13 changes: 12 additions & 1 deletion modules/Auth/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
use Illuminate\Notifications\Notifiable;
use Modules\Auth\Support\Role;
use Modules\Auth\Traits\HasRole;
use Tymon\JWTAuth\Contracts\JWTSubject;

final class User extends Authenticatable
final class User extends Authenticatable implements JWTSubject
{
use CommonQueries,
HasFactory,
Expand Down Expand Up @@ -51,6 +52,16 @@ final class User extends Authenticatable
'role' => Role::REGULAR,
];

public function getJWTIdentifier(): mixed
{
return $this->getKey();
}

public function getJWTCustomClaims(): array
{
return [];
}

protected static function booted(): void
{
self::addGlobalScope(
Expand Down
4 changes: 2 additions & 2 deletions tests/Feature/ApplicationUpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
expect($this->get('/')->json())
->toBe([
'application' => config('app.name'),
'status' => Response::HTTP_OK,
'datetime' => Carbon::now()->format(Formatter::API_DATETIME_FORMAT),
'environment' => config('app.env'),
'php_version' => phpversion(),
'laravel_version' => App::version(),
'status' => Response::HTTP_OK,
'datetime' => Carbon::now()->format(Formatter::API_DATETIME_FORMAT),
]);
});
2 changes: 1 addition & 1 deletion tests/Feature/Auth/DeleteUserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
expect($this->actingAs(testUser(Role::ADMIN))->delete("v1/users/{$newUser->uuid}"))
->assertNoContent();

$this->assertDatabaseMissing(User::getModelTable(), [
$this->assertSoftDeleted(User::getModelTable(), [
'email' => $newUser->email,
]);
});
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/Auth/FetchUserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
'uuid' => $newUser->uuid,
'name' => $newUser->name,
'email' => $newUser->email,
'role' => $newUser->role,
'role' => $newUser->role->value,
'active' => $newUser->active,
'created_at' => $newUser->created_at->toISOString(),
'updated_at' => $newUser->updated_at->toISOString(),
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/Auth/LoggedUserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
'uuid' => $user->uuid,
'name' => $user->name,
'email' => $user->email,
'role' => $user->role,
'role' => $user->role->value,
'active' => $user->active,
'created_at' => $user->created_at->toISOString(),
'updated_at' => $user->updated_at->toISOString(),
Expand Down
12 changes: 0 additions & 12 deletions tests/Feature/Auth/LogoutTest.php

This file was deleted.

0 comments on commit 8a99a4c

Please sign in to comment.