feat: 首次登录引导与注册修复

This commit is contained in:
SpecialX
2026-01-12 10:49:30 +08:00
parent 15fcf2bc78
commit 8577280ab2
12 changed files with 653 additions and 25 deletions

View File

@@ -49,13 +49,37 @@ export const { handlers, auth, signIn, signOut } = NextAuth({
if (user) {
token.id = (user as { id: string }).id
token.role = (user as { role?: string }).role ?? "student"
token.name = (user as { name?: string }).name
}
const userId = String(token.id ?? "").trim()
if (userId) {
const [{ eq }, { db }, { users }] = await Promise.all([
import("drizzle-orm"),
import("@/shared/db"),
import("@/shared/db/schema"),
])
const fresh = await db.query.users.findFirst({
where: eq(users.id, userId),
columns: { role: true, name: true },
})
if (fresh) {
token.role = fresh.role ?? token.role ?? "student"
token.name = fresh.name ?? token.name
}
}
return token
},
session: async ({ session, token }) => {
if (session.user) {
session.user.id = String(token.id ?? "")
session.user.role = String(token.role ?? "student")
if (typeof token.name === "string") {
session.user.name = token.name
}
}
return session
},