24 lines
803 B
TypeScript
24 lines
803 B
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { djangoUrl, setAuthCookies } from "../proxy";
|
|
|
|
export async function POST(req: NextRequest) {
|
|
const cookieRefresh = req.cookies.get("refresh_token")?.value;
|
|
const body = await req.text().catch(() => "");
|
|
let parsed: any = {};
|
|
try {
|
|
parsed = body ? JSON.parse(body) : {};
|
|
} catch {
|
|
parsed = {};
|
|
}
|
|
const refresh = parsed.refresh || cookieRefresh || "";
|
|
const upstream = await fetch(djangoUrl("auth/refresh/"), {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ refresh }),
|
|
});
|
|
const data = await upstream.json().catch(() => ({}));
|
|
const res = NextResponse.json(data, { status: upstream.status });
|
|
if (upstream.ok) setAuthCookies(res, data);
|
|
return res;
|
|
}
|