38 lines
1.1 KiB
TypeScript
38 lines
1.1 KiB
TypeScript
import { defineEventHandler, createError } from 'h3'
|
|
import { getSubdomainFromRequest } from '~/server/utils/tenant'
|
|
import { getSessionToken, clearSessionCookie, clearTenantIdCookie } from '~/server/utils/session'
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
const config = useRuntimeConfig()
|
|
const subdomain = getSubdomainFromRequest(event)
|
|
const token = getSessionToken(event)
|
|
|
|
const backendUrl = config.backendUrl || 'http://localhost:3000'
|
|
|
|
try {
|
|
// Call backend logout endpoint if we have a token
|
|
if (token) {
|
|
await fetch(`${backendUrl}/api/auth/logout`, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': `Bearer ${token}`,
|
|
...(subdomain && { 'x-tenant-subdomain': subdomain }),
|
|
},
|
|
})
|
|
}
|
|
} catch (error) {
|
|
// Log but don't fail - we still want to clear cookies
|
|
console.error('Backend logout error:', error)
|
|
}
|
|
|
|
// Always clear cookies regardless of backend response
|
|
clearSessionCookie(event)
|
|
clearTenantIdCookie(event)
|
|
|
|
return {
|
|
success: true,
|
|
message: 'Logged out successfully',
|
|
}
|
|
})
|