WIP - BFF
This commit is contained in:
37
frontend/server/api/auth/logout.post.ts
Normal file
37
frontend/server/api/auth/logout.post.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
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',
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user