2024-12-13 16:37:20 +01:00
|
|
|
import { NextRequest, NextResponse } from "next/server";
|
2025-02-05 14:09:59 +01:00
|
|
|
import { Redis } from "ioredis";
|
|
|
|
|
import { getToken } from "next-auth/jwt";
|
2024-12-13 16:37:20 +01:00
|
|
|
|
|
|
|
|
export async function POST(request: NextRequest) {
|
2025-02-05 14:09:59 +01:00
|
|
|
const token = await getToken({
|
|
|
|
|
req: request,
|
|
|
|
|
secret: process.env.NEXTAUTH_SECRET,
|
|
|
|
|
});
|
2024-12-13 16:37:20 +01:00
|
|
|
const allCookies = request.cookies.getAll();
|
|
|
|
|
const zammadURL = process.env.ZAMMAD_URL ?? "http://zammad-nginx:8080";
|
|
|
|
|
const signOutURL = `${zammadURL}/api/v1/signout`;
|
|
|
|
|
const headers = {
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
Accept: "application/json",
|
|
|
|
|
Cookie: allCookies
|
|
|
|
|
.map((cookie) => `${cookie.name}=${cookie.value}`)
|
|
|
|
|
.join("; "),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
await fetch(signOutURL, { headers });
|
|
|
|
|
|
|
|
|
|
const cookiePrefixesToRemove = ["_zammad"];
|
|
|
|
|
const response = NextResponse.json({ message: "ok" });
|
|
|
|
|
|
|
|
|
|
for (const cookie of allCookies) {
|
|
|
|
|
if (
|
|
|
|
|
cookiePrefixesToRemove.some((prefix) => cookie.name.startsWith(prefix))
|
|
|
|
|
) {
|
|
|
|
|
response.cookies.set(cookie.name, "", { path: "/", maxAge: 0 });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-05 14:09:59 +01:00
|
|
|
const redis = new Redis(process.env.REDIS_URL);
|
|
|
|
|
await redis.setex(`invalidated:${token.sub}`, 24 * 60 * 60, "1");
|
|
|
|
|
|
2024-12-13 16:37:20 +01:00
|
|
|
return response;
|
|
|
|
|
}
|