260615_04_ENH: kon용 api에 cors 조건 완화
This commit is contained in:
@@ -6,6 +6,13 @@ import {PerformanceCompact} from "@/types/gwcf";
|
||||
|
||||
const ALLOWED_ORIGIN = "https://kon.kric.com";
|
||||
|
||||
const corsHeaders = {
|
||||
"Access-Control-Allow-Origin": ALLOWED_ORIGIN,
|
||||
"Access-Control-Allow-Methods": "POST, OPTIONS",
|
||||
"Access-Control-Allow-Headers": "Content-Type, Authorization",
|
||||
"Vary": "Origin",
|
||||
};
|
||||
|
||||
const getRequestOrigin = (req: NextRequest) => {
|
||||
const origin = req.headers.get("origin");
|
||||
if (origin) {
|
||||
@@ -28,13 +35,31 @@ const isAllowedOrigin = (req: NextRequest) => {
|
||||
return getRequestOrigin(req) === ALLOWED_ORIGIN;
|
||||
};
|
||||
|
||||
const withCors = (response: NextResponse) => {
|
||||
Object.entries(corsHeaders).forEach(([key, value]) => {
|
||||
response.headers.set(key, value);
|
||||
});
|
||||
return response;
|
||||
};
|
||||
|
||||
export const OPTIONS = async (req: NextRequest) => {
|
||||
if (!isAllowedOrigin(req)) {
|
||||
return withCors(NextResponse.json({ message: "Forbidden" }, { status: 403 }));
|
||||
}
|
||||
|
||||
return new NextResponse(null, {
|
||||
status: 204,
|
||||
headers: corsHeaders,
|
||||
});
|
||||
};
|
||||
|
||||
export const POST = async (
|
||||
req: NextRequest,
|
||||
context: RouteContext
|
||||
) => {
|
||||
try {
|
||||
if (!isAllowedOrigin(req)) {
|
||||
return NextResponse.json({ message: "Forbidden" }, { status: 403 });
|
||||
return withCors(NextResponse.json({ message: "Forbidden" }, { status: 403 }));
|
||||
}
|
||||
|
||||
const {slug = []} = await context.params;
|
||||
@@ -43,17 +68,17 @@ export const POST = async (
|
||||
|
||||
if(action === "assign-confirm" && slug.length === 2) {
|
||||
const result = await backApi.post<boolean>(`/assign/confirm/${slug[1]}`)
|
||||
return NextResponse.json(result, { status: 200 });
|
||||
return withCors(NextResponse.json(result, { status: 200 }));
|
||||
}
|
||||
|
||||
if(action === "performance-list") {
|
||||
const result = await backApi.get<PerformanceCompact[]>(`/performance/list/compact`)
|
||||
return NextResponse.json(result, { status: 200 });
|
||||
return withCors(NextResponse.json(result, { status: 200 }));
|
||||
}
|
||||
|
||||
return NextResponse.json({ message: "Not found" }, { status: 404 });
|
||||
return withCors(NextResponse.json({ message: "Not found" }, { status: 404 }));
|
||||
|
||||
} catch (error: unknown) {
|
||||
return errorToResponse(error);
|
||||
return withCors(errorToResponse(error));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user