summaryrefslogtreecommitdiff
path: root/frontend/app/api/logout/route.ts
blob: 202a8b7d6dafaa120709f45c8b26f2ad3bfccc4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { NextRequest } from 'next/server';
import { redirect } from 'next/navigation';
import { Auth_tinyauth_endpoint, Auth_tinyauth_public_endpoint } from '@/lib/auth_shared';

export async function POST(request: NextRequest) {
  try {
    // Get the current session cookie
    const cookies = request.cookies.getAll();
    const sessionCookie = cookies.find(cookie => cookie.name.includes('tinyauth-session'));
    
    if (sessionCookie) {
      // Call tinyauth logout endpoint to invalidate the session
      await fetch(`${Auth_tinyauth_endpoint()}/auth/logout`, {
        method: 'POST',
        headers: {
          'Cookie': `${sessionCookie.name}=${sessionCookie.value}`
        }
      });
      
      // Note: We don't need to check the response status as we'll redirect anyway
    }
    
    // Redirect to the public logout endpoint which should clear cookies client-side
    const publicLogoutUrl = `${Auth_tinyauth_public_endpoint()}/auth/logout`;
    return Response.redirect(publicLogoutUrl, 302);
    
  } catch (error) {
    console.error('Logout error:', error);
    // Even if logout fails, redirect to home
    return redirect('/');
  }
}