diff options
Diffstat (limited to 'frontend/app/api/logout/route.ts')
| -rw-r--r-- | frontend/app/api/logout/route.ts | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/frontend/app/api/logout/route.ts b/frontend/app/api/logout/route.ts new file mode 100644 index 0000000..51de324 --- /dev/null +++ b/frontend/app/api/logout/route.ts | |||
| @@ -0,0 +1,32 @@ | |||
| 1 | import { NextRequest } from 'next/server'; | ||
| 2 | import { redirect } from 'next/navigation'; | ||
| 3 | import { Auth_tinyauth_endpoint, Auth_tinyauth_public_endpoint } from '@/lib/auth_shared'; | ||
| 4 | |||
| 5 | export async function POST(request: NextRequest) { | ||
| 6 | try { | ||
| 7 | // Get the current session cookie | ||
| 8 | const cookies = request.cookies.getAll(); | ||
| 9 | const sessionCookie = cookies.find(cookie => cookie.name.includes('tinyauth-session')); | ||
| 10 | |||
| 11 | if (sessionCookie) { | ||
| 12 | // Call tinyauth logout endpoint to invalidate the session | ||
| 13 | const logoutResponse = await fetch(`${Auth_tinyauth_endpoint()}/auth/logout`, { | ||
| 14 | method: 'POST', | ||
| 15 | headers: { | ||
| 16 | 'Cookie': `${sessionCookie.name}=${sessionCookie.value}` | ||
| 17 | } | ||
| 18 | }); | ||
| 19 | |||
| 20 | // Note: We don't need to check the response status as we'll redirect anyway | ||
| 21 | } | ||
| 22 | |||
| 23 | // Redirect to the public logout endpoint which should clear cookies client-side | ||
| 24 | const publicLogoutUrl = `${Auth_tinyauth_public_endpoint()}/auth/logout`; | ||
| 25 | return Response.redirect(publicLogoutUrl, 302); | ||
| 26 | |||
| 27 | } catch (error) { | ||
| 28 | console.error('Logout error:', error); | ||
| 29 | // Even if logout fails, redirect to home | ||
| 30 | return redirect('/'); | ||
| 31 | } | ||
| 32 | } \ No newline at end of file | ||
