gh_UserManager/apps/web/app/api/auth/proxy.ts
bermooda-company 54d5891edf user
2026-08-23 23:59:14 +03:30

46 lines
1.1 KiB
TypeScript

import { NextResponse } from "next/server";
const API_BASE =
process.env.NEXT_PUBLIC_API_URL || "http://localhost:8000/api/v1";
export function djangoUrl(path: string): string {
return `${API_BASE}/${path}`;
}
function cookieBase() {
const secure = process.env.NODE_ENV === "production";
return {
httpOnly: true,
secure,
sameSite: "lax" as const,
path: "/",
};
}
export function setAuthCookies(res: NextResponse, data: any) {
if (data?.access) {
res.cookies.set("access_token", data.access, {
...cookieBase(),
maxAge: 60 * 30,
});
}
if (data?.refresh) {
res.cookies.set("refresh_token", data.refresh, {
...cookieBase(),
maxAge: 60 * 60 * 24 * 30,
});
}
if (data?.session_id) {
res.cookies.set("session_id", data.session_id, {
...cookieBase(),
maxAge: 60 * 60 * 24 * 30,
});
}
}
export function clearAuthCookies(res: NextResponse) {
for (const name of ["access_token", "refresh_token", "session_id"]) {
res.cookies.set(name, "", { ...cookieBase(), maxAge: 0 });
}
}