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

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;
}