feat: 完善鉴权
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
// server/middleware/api-auth.ts - 统一 API 鉴权层,所有非公开 /api/* 请求都必须通过此处
|
||||
import { createError, getRequestURL } from "h3";
|
||||
|
||||
import { isPublicApiRoute } from "~~/server/utils/api-auth-rules";
|
||||
import { getAuthSession } from "~~/server/utils/auth";
|
||||
|
||||
/**
|
||||
* 统一 API 鉴权 middleware
|
||||
*
|
||||
* 策略:默认所有 /api/* 需要登录,只有 publicApiRoutes 里的路由可以公开访问。
|
||||
* 鉴权通过后将 session 挂到 event.context.auth,handler 直接取用,无需重复查询。
|
||||
*
|
||||
* 不影响页面渲染、静态资源或 Nuxt 内部请求。
|
||||
*/
|
||||
export default defineEventHandler(async (event) => {
|
||||
const { pathname } = getRequestURL(event);
|
||||
const method = event.method;
|
||||
|
||||
// 只处理 /api/ 路径
|
||||
if (!pathname.startsWith("/api/")) return;
|
||||
|
||||
// OPTIONS 预检请求不需要鉴权,CORS headers 由 nuxt.config routeRules 统一设置
|
||||
if (method === "OPTIONS") return;
|
||||
|
||||
// 公开路由直接放行
|
||||
if (isPublicApiRoute(pathname, method)) return;
|
||||
|
||||
const session = await getAuthSession(event);
|
||||
if (!session) {
|
||||
throw createError({ statusCode: 401, statusMessage: "未登录" });
|
||||
}
|
||||
|
||||
// 挂到 event.context,后续 handler 直接读取,不再重复查询 session
|
||||
event.context.auth = session;
|
||||
});
|
||||
Reference in New Issue
Block a user