@@ -0,0 +1,22 @@
|
||||
// server/middleware/registration-guard.ts - 注册开关守卫
|
||||
//
|
||||
// 拦截 POST /api/auth/sign-up/email,若系统配置关闭了注册,立即返回 403。
|
||||
// 此中间件优先于 Better Auth catch-all handler 执行,无需改动 auth.ts。
|
||||
|
||||
import { getRequestURL, setResponseStatus } from "h3";
|
||||
|
||||
import { apiErr } from "~~/server/utils/response";
|
||||
import { getSysConfig } from "~~/server/utils/sysConfig";
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const { pathname } = getRequestURL(event);
|
||||
|
||||
// 只拦截注册接口
|
||||
if (pathname !== "/api/auth/sign-up/email" || event.method !== "POST") return;
|
||||
|
||||
const cfg = await getSysConfig();
|
||||
if (!cfg.allowRegistration) {
|
||||
setResponseStatus(event, 403);
|
||||
return apiErr(403, "当前未开启注册");
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user