Files
QQuiz/web/src/app/frontend-api/auth/me/route.ts
handsomezhuzhu 9a1a9d3247 refactor: remove legacy frontend code and implement new Next.js structure
- Deleted the old Register page and utility functions.
- Removed Tailwind CSS configuration and Vite configuration files.
- Added a new script for starting a single container with FastAPI and Next.js.
- Updated README to reflect the current status of the Next.js frontend.
- Implemented new login and registration API routes with improved error handling.
- Refactored frontend API calls to use the new proxy structure.
- Enhanced error handling in API response processing.
- Updated components to align with the new API endpoints and structure.
2026-04-17 21:15:06 +08:00

58 lines
1.3 KiB
TypeScript

import { cookies } from "next/headers";
import { NextResponse } from "next/server";
import {
SESSION_COOKIE_NAME,
buildBackendUrl
} from "@/lib/api/config";
import {
getResponseErrorMessage,
isRecord,
readResponsePayload
} from "@/lib/api/response";
export async function GET() {
const token = cookies().get(SESSION_COOKIE_NAME)?.value;
if (!token) {
return NextResponse.json({ detail: "Unauthorized" }, { status: 401 });
}
let response: Response;
try {
response = await fetch(buildBackendUrl("/auth/me"), {
headers: {
Authorization: `Bearer ${token}`
},
cache: "no-store"
});
} catch {
return NextResponse.json(
{ detail: "Backend API is unavailable." },
{ status: 502 }
);
}
const payload = await readResponsePayload(response);
if (response.status === 401) {
cookies().delete(SESSION_COOKIE_NAME);
}
if (!response.ok) {
return NextResponse.json(
{ detail: getResponseErrorMessage(payload, "获取当前用户失败") },
{ status: response.status }
);
}
if (!isRecord(payload)) {
return NextResponse.json(
{ detail: "Backend returned an invalid auth response." },
{ status: 502 }
);
}
return NextResponse.json(payload, { status: response.status });
}