fix: improve error handling for session auth check
This commit is contained in:
@@ -71,14 +71,16 @@ const app = new Elysia()
|
|||||||
.use(taskRoutes)
|
.use(taskRoutes)
|
||||||
.get("/health", () => ({ status: "ok", service: "hammer-queue" }))
|
.get("/health", () => ({ status: "ok", service: "hammer-queue" }))
|
||||||
.onError(({ error, set }) => {
|
.onError(({ error, set }) => {
|
||||||
if (error.message === "Unauthorized") {
|
const msg = error?.message || String(error);
|
||||||
|
if (msg === "Unauthorized") {
|
||||||
set.status = 401;
|
set.status = 401;
|
||||||
return { error: "Unauthorized" };
|
return { error: "Unauthorized" };
|
||||||
}
|
}
|
||||||
if (error.message === "Task not found") {
|
if (msg === "Task not found") {
|
||||||
set.status = 404;
|
set.status = 404;
|
||||||
return { error: "Task not found" };
|
return { error: "Task not found" };
|
||||||
}
|
}
|
||||||
|
console.error("Unhandled error:", msg);
|
||||||
set.status = 500;
|
set.status = 500;
|
||||||
return { error: "Internal server error" };
|
return { error: "Internal server error" };
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -28,8 +28,13 @@ async function requireSessionOrBearer(request: Request, headers: Record<string,
|
|||||||
if (authHeader === `Bearer ${BEARER_TOKEN}`) return;
|
if (authHeader === `Bearer ${BEARER_TOKEN}`) return;
|
||||||
|
|
||||||
// Check session
|
// Check session
|
||||||
const session = await auth.api.getSession({ headers: request.headers });
|
try {
|
||||||
if (!session) throw new Error("Unauthorized");
|
const session = await auth.api.getSession({ headers: request.headers });
|
||||||
|
if (session) return;
|
||||||
|
} catch {
|
||||||
|
// Session check failed
|
||||||
|
}
|
||||||
|
throw new Error("Unauthorized");
|
||||||
}
|
}
|
||||||
|
|
||||||
export const taskRoutes = new Elysia({ prefix: "/api/tasks" })
|
export const taskRoutes = new Elysia({ prefix: "/api/tasks" })
|
||||||
|
|||||||
Reference in New Issue
Block a user