2 changed files with 82 additions and 5 deletions
@ -0,0 +1,60 @@ |
|||||
|
import { NextRequest, NextResponse } from "next/server"; |
||||
|
import { getRequestToken } from "@/app/api/_auth"; |
||||
|
|
||||
|
interface UpstreamAuthResponse { |
||||
|
data?: unknown; |
||||
|
message?: unknown; |
||||
|
status?: unknown; |
||||
|
} |
||||
|
|
||||
|
const UPSTREAM_LOGOUT_PATH = "/api_client/auth/logout"; |
||||
|
|
||||
|
function getUpstreamUrl(request: NextRequest): string { |
||||
|
const baseUrl = |
||||
|
process.env.POPIART_API_BASE_URL || |
||||
|
process.env.NEXT_PUBLIC_APP_URL || |
||||
|
request.nextUrl.origin; |
||||
|
return baseUrl.replace(/\/+$/, "") + UPSTREAM_LOGOUT_PATH; |
||||
|
} |
||||
|
|
||||
|
async function logout(request: NextRequest, method: "GET" | "POST") { |
||||
|
try { |
||||
|
const token = getRequestToken(request); |
||||
|
const headers: Record<string, string> = { |
||||
|
Accept: "application/json", |
||||
|
}; |
||||
|
|
||||
|
if (token) { |
||||
|
headers.Authorization = `Bearer ${token}`; |
||||
|
headers.token = token; |
||||
|
} |
||||
|
|
||||
|
const response = await fetch(getUpstreamUrl(request), { |
||||
|
method, |
||||
|
headers, |
||||
|
cache: "no-store", |
||||
|
}); |
||||
|
|
||||
|
const responseBody = (await response.json()) as UpstreamAuthResponse; |
||||
|
return NextResponse.json<UpstreamAuthResponse>(responseBody, { |
||||
|
status: response.status, |
||||
|
}); |
||||
|
} catch (error) { |
||||
|
console.error("Error logging out:", error); |
||||
|
return NextResponse.json<UpstreamAuthResponse>( |
||||
|
{ |
||||
|
status: "0500", |
||||
|
message: "Failed to logout", |
||||
|
}, |
||||
|
{ status: 500 } |
||||
|
); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
export async function POST(request: NextRequest) { |
||||
|
return logout(request, "POST"); |
||||
|
} |
||||
|
|
||||
|
export async function GET(request: NextRequest) { |
||||
|
return logout(request, "GET"); |
||||
|
} |
||||
Loading…
Reference in new issue