From 0bbb61463ff7bcf35086cde84a87f8753ff99a77 Mon Sep 17 00:00:00 2001 From: Valentin Sickert <17144397+Lapotor@users.noreply.github.com> Date: Sun, 10 Dec 2023 23:10:15 +0100 Subject: [PATCH 1/3] feat(auth): add `/logout` endpoint Signed-off-by: Valentin Sickert <17144397+Lapotor@users.noreply.github.com> --- app/Http/Controllers/AuthController.php | 19 +++++++++++++++++++ routes/api/v1/auth.php | 1 + 2 files changed, 20 insertions(+) diff --git a/app/Http/Controllers/AuthController.php b/app/Http/Controllers/AuthController.php index 39dc4d6e..e324db99 100644 --- a/app/Http/Controllers/AuthController.php +++ b/app/Http/Controllers/AuthController.php @@ -2,8 +2,10 @@ namespace App\Http\Controllers; +use App\Http\Responses\ApiSuccessResponse; use Illuminate\Http\Request; use Illuminate\Http\Response; +use Illuminate\Support\Facades\Auth; class AuthController extends Controller { @@ -35,4 +37,21 @@ public function login(Request $request) 'access_token' => $token ], Response::HTTP_OK); } + + /** + * Logout the user. + * + * @param \Illuminate\Http\Request $request + * @return \App\Http\Responses\ApiSuccessResponse + */ + public function logout(Request $request) + { + if (!$request->user()) { + return new ApiSuccessResponse('User logged out successfully'); + } + + $request->user()->currentAccessToken()->delete(); + + return new ApiSuccessResponse('User logged out successfully'); + } } diff --git a/routes/api/v1/auth.php b/routes/api/v1/auth.php index 8ac55dfc..85f1effb 100644 --- a/routes/api/v1/auth.php +++ b/routes/api/v1/auth.php @@ -4,3 +4,4 @@ use Illuminate\Support\Facades\Route; Route::post('/login', [AuthController::class, 'login']); +Route::post('/logout', [AuthController::class, 'logout'])->middleware('auth:sanctum'); From 741df46958a7d2a4dfdff318b4d47014b0ca9bcc Mon Sep 17 00:00:00 2001 From: Valentin Sickert <17144397+Lapotor@users.noreply.github.com> Date: Sun, 10 Dec 2023 23:29:28 +0100 Subject: [PATCH 2/3] test(auth): add test for logout Signed-off-by: Valentin Sickert <17144397+Lapotor@users.noreply.github.com> --- tests/Feature/AuthControllerTest.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/Feature/AuthControllerTest.php b/tests/Feature/AuthControllerTest.php index 60394e6c..e9662830 100644 --- a/tests/Feature/AuthControllerTest.php +++ b/tests/Feature/AuthControllerTest.php @@ -6,8 +6,11 @@ use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\WithFaker; use Illuminate\Http\Response; +use Laravel\Sanctum\Sanctum; use Tests\TestCase; +use function Psy\debug; + class AuthControllerTest extends TestCase { use RefreshDatabase, WithFaker; @@ -67,4 +70,25 @@ public function test_login_with_invalid_credentials(): void $this->assertGuest(); } + + /** + * Test logout. + * + * @return void + */ + public function test_logout(): void + { + + Sanctum::actingAs( + User::factory()->create(), + ['*'] + ); + + $response = $this->postJson('/api/v1/logout'); + + $response->assertStatus(Response::HTTP_OK) + ->assertJson([ + 'data' => 'User logged out successfully', + ]); + } } From 0e70568ced49812c968abc97a44d7cdc673f6f86 Mon Sep 17 00:00:00 2001 From: Valentin Sickert <17144397+Lapotor@users.noreply.github.com> Date: Sun, 10 Dec 2023 23:29:49 +0100 Subject: [PATCH 3/3] change response when no user found Signed-off-by: Valentin Sickert <17144397+Lapotor@users.noreply.github.com> --- app/Http/Controllers/AuthController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Http/Controllers/AuthController.php b/app/Http/Controllers/AuthController.php index e324db99..3ce61adf 100644 --- a/app/Http/Controllers/AuthController.php +++ b/app/Http/Controllers/AuthController.php @@ -47,7 +47,7 @@ public function login(Request $request) public function logout(Request $request) { if (!$request->user()) { - return new ApiSuccessResponse('User logged out successfully'); + return new ApiSuccessResponse('No user found'); } $request->user()->currentAccessToken()->delete();