feat(auth): 强制要求 username 并- 添加 hooks 验证 username 必填

- 修改 schema: username 改为 NOT NULL
- 重置本地和生产数据库
This commit is contained in:
2026-03-10 09:45:15 +08:00
parent 5406543cbe
commit d9fd09c13d
12 changed files with 277 additions and 455 deletions

View File

@@ -1,8 +1,9 @@
import { betterAuth } from "better-auth";
import { prismaAdapter } from "better-auth/adapters/prisma";
import { nextCookies } from "better-auth/next-js";
import { prisma } from "./lib/db";
import { username } from "better-auth/plugins";
import { createAuthMiddleware, APIError } from "better-auth/api";
import { prisma } from "./lib/db";
import {
sendEmail,
generateVerificationEmailHtml,
@@ -41,4 +42,16 @@ export const auth = betterAuth({
},
},
plugins: [nextCookies(), username()],
hooks: {
before: createAuthMiddleware(async (ctx) => {
if (ctx.path !== "/sign-up/email" && ctx.path !== "/update-user") return;
const body = ctx.body as { username?: string };
if (!body.username || body.username.trim() === "") {
throw new APIError("BAD_REQUEST", {
message: "Username is required",
});
}
}),
},
});