Skip to content

Commit

Permalink
feat: Add logout
Browse files Browse the repository at this point in the history
  • Loading branch information
vinidotruan committed Dec 2, 2024
1 parent da0ae4e commit 55c98af
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
11 changes: 5 additions & 6 deletions app/Http/Controllers/TwitchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
}
14 changes: 8 additions & 6 deletions app/Services/TwitchService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

}
}
2 changes: 2 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 55c98af

Please sign in to comment.