mirror of
https://github.com/lifegpc/eh-downloader.git
synced 2026-07-08 01:31:00 +08:00
Fix 401 cause CORS not works
This commit is contained in:
@@ -39,7 +39,14 @@ function handle_auth(req: Request, ctx: MiddlewareHandlerContext) {
|
||||
export async function handler(req: Request, ctx: MiddlewareHandlerContext) {
|
||||
const m = get_task_manager();
|
||||
if (!(handle_auth(req, ctx))) {
|
||||
return return_error(401, "Unauthorized");
|
||||
const headers: HeadersInit = {};
|
||||
const origin = req.headers.get("origin");
|
||||
if (origin) {
|
||||
const c = m.cfg.cors_credentials_hosts.includes(origin);
|
||||
headers["Access-Control-Allow-Origin"] = c ? origin : "*";
|
||||
if (c) headers["Access-Control-Allow-Credentials"] = "true";
|
||||
}
|
||||
return return_error(401, "Unauthorized", 401, headers);
|
||||
}
|
||||
const res = await ctx.next();
|
||||
if (req.method === "OPTIONS" && res.status === 405) {
|
||||
|
||||
@@ -29,8 +29,10 @@ function gen_response<T>(
|
||||
export function return_error<T = unknown>(
|
||||
status: Exclude<number, 0>,
|
||||
error: string,
|
||||
http_status = 200,
|
||||
headers: HeadersInit = {},
|
||||
) {
|
||||
return gen_response<T>({ ok: false, status, error });
|
||||
return gen_response<T>({ ok: false, status, error }, http_status, headers);
|
||||
}
|
||||
|
||||
export function return_data<T = unknown>(
|
||||
|
||||
Reference in New Issue
Block a user