feat: add lint

Signed-off-by: d0zingcat <iamtangli42@gmail.com>
This commit is contained in:
2026-01-14 19:36:46 +08:00
parent f414264050
commit 451793f6ce
41 changed files with 3724 additions and 3017 deletions

View File

@@ -1,51 +1,58 @@
import { Context, Next } from 'hono';
import { getCookie } from 'hono/cookie';
import type { Context, Next } from "hono";
import { getCookie } from "hono/cookie";
export interface AuthSession {
id: string;
name: string;
email: string | null;
isAdmin: boolean;
id: string;
name: string;
email: string | null;
isAdmin: boolean;
}
export async function requireAuth(c: Context, next: Next) {
const sessionCookie = getCookie(c, 'session');
const sessionCookie = getCookie(c, "session");
if (!sessionCookie) {
return c.json({ error: 'Authentication required' }, 401);
}
if (!sessionCookie) {
return c.json({ error: "Authentication required" }, 401);
}
try {
const session: AuthSession = sessionCookie ? JSON.parse(sessionCookie) : null;
if (!session) {
return c.json({ error: 'Authentication required' }, 401);
}
c.set('session', session);
await next();
} catch (error) {
console.error('[Middleware] Failed to parse session cookie:', error);
return c.json({ error: 'Invalid session' }, 401);
}
try {
const session: AuthSession = sessionCookie
? JSON.parse(sessionCookie)
: null;
if (!session) {
return c.json({ error: "Authentication required" }, 401);
}
c.set("session", session);
await next();
} catch (error) {
console.error("[Middleware] Failed to parse session cookie:", error);
return c.json({ error: "Invalid session" }, 401);
}
}
export async function requireAdmin(c: Context, next: Next) {
const sessionCookie = getCookie(c, 'session');
const sessionCookie = getCookie(c, "session");
if (!sessionCookie) {
return c.json({ error: 'Authentication required' }, 401);
}
if (!sessionCookie) {
return c.json({ error: "Authentication required" }, 401);
}
try {
const session: AuthSession = sessionCookie ? JSON.parse(sessionCookie) : null;
try {
const session: AuthSession = sessionCookie
? JSON.parse(sessionCookie)
: null;
if (!session || !session.isAdmin) {
return c.json({ error: 'Admin access required' }, 403);
}
if (!session || !session.isAdmin) {
return c.json({ error: "Admin access required" }, 403);
}
c.set('session', session);
await next();
} catch (error) {
console.error('[Middleware] Failed to parse session cookie in requireAdmin:', error);
return c.json({ error: 'Invalid session' }, 401);
}
c.set("session", session);
await next();
} catch (error) {
console.error(
"[Middleware] Failed to parse session cookie in requireAdmin:",
error,
);
return c.json({ error: "Invalid session" }, 401);
}
}