mirror of
https://github.com/d0zingcat/alert-message-center.git
synced 2026-05-30 07:26:46 +00:00
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user