From 55c98af94943ada94343986a1f912e3dbcb5effd Mon Sep 17 00:00:00 2001 From: Vini Date: Mon, 2 Dec 2024 17:21:03 -0300 Subject: [PATCH] feat: Add logout --- app/Http/Controllers/TwitchController.php | 11 +++++------ app/Services/TwitchService.php | 14 ++++++++------ routes/api.php | 2 ++ 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/app/Http/Controllers/TwitchController.php b/app/Http/Controllers/TwitchController.php index c92f165..d81b680 100644 --- a/app/Http/Controllers/TwitchController.php +++ b/app/Http/Controllers/TwitchController.php @@ -25,12 +25,11 @@ public function getAuthUrl(Request $request) { return response()->json("https://id.twitch.tv/oauth2/authorize?force_verify=true&client_id={$client_id}&response_type=code&redirect_uri={$redirect_uri}&scope=user:read:email&claims=email"); } - /** - * @throws \Exception - */ - public function getFollowedChannels(Request $request, TwitchService $twitchService): JsonResponse + public function logout(Request $request, TwitchService $twitchService) { - $data = $twitchService->handleFollowedChannels(); - return response()->json($data); + auth()->user()->currentAccessToken()->delete(); + $twitchService->logout(); + + return response()->json(['data' => 'success']); } } diff --git a/app/Services/TwitchService.php b/app/Services/TwitchService.php index c878e76..932aacd 100644 --- a/app/Services/TwitchService.php +++ b/app/Services/TwitchService.php @@ -69,15 +69,17 @@ public function handleCallback(string $code): array ]; } - public function handleFollowedChannels(): \Illuminate\Http\JsonResponse - { - $userId = session('twitch_id'); - $response = Http::get(`https://api.twitch.tv/helix/channels/followed?user_id=$userId`); + public function logout() { + $response = Http::asForm()->post('https://id.twitch.tv/oauth2/revoke', [ + 'client_id' => $this->clientId, + 'token' => auth()->user()->twitch_access_token + ]); if(!$response->successful()) { - throw new Exception('Falha ao obter canais seguidos: ' . $response->body()); + throw new Exception("Error Processing Request", $response->body()); } - return response()->json(); + return true; + } } diff --git a/routes/api.php b/routes/api.php index 301f91d..e5e929a 100644 --- a/routes/api.php +++ b/routes/api.php @@ -11,10 +11,12 @@ Route::middleware('auth:sanctum')->group(function() { Route::prefix('sessions')->group(function () { + Route::post('', [StudySessionController::class, 'store']); Route::post('{id}/start', [StudySessionController::class, 'start']); Route::get('{studySession:uri}', [StudySessionController::class, 'show']); Route::get('', [StudySessionController::class, 'index']); }); + Route::post('auth/logout', [TwitchController::class, 'logout']); }); Route::middleware('auth:sanctum')->get('/user', function (Request $request) { return $request->user();