feat(auth): enhance user schema with role and ban features

This commit is contained in:
2025-11-17 04:28:50 +08:00
parent 86595c7639
commit 68d5c596b4
3 changed files with 26 additions and 12 deletions

View File

@@ -11,9 +11,12 @@ export const user = pgTable("user", {
.defaultNow() .defaultNow()
.$onUpdate(() => /* @__PURE__ */ new Date()) .$onUpdate(() => /* @__PURE__ */ new Date())
.notNull(), .notNull(),
role: text("role"),
banned: boolean("banned").default(false),
banReason: text("ban_reason"),
banExpires: timestamp("ban_expires"),
username: text("username").unique(), username: text("username").unique(),
displayUsername: text("display_username"), displayUsername: text("display_username"),
role: text("role").default("user"),
}); });
export const session = pgTable("session", { export const session = pgTable("session", {
@@ -29,6 +32,7 @@ export const session = pgTable("session", {
userId: text("user_id") userId: text("user_id")
.notNull() .notNull()
.references(() => user.id, { onDelete: "cascade" }), .references(() => user.id, { onDelete: "cascade" }),
impersonatedBy: text("impersonated_by"),
}); });
export const account = pgTable("account", { export const account = pgTable("account", {

View File

@@ -46,10 +46,23 @@ app.use(
app.on(['POST', 'GET'], '/api/auth/*', (c) => auth.handler(c.req.raw)) app.on(['POST', 'GET'], '/api/auth/*', (c) => auth.handler(c.req.raw))
app.get('/', (c) => { app.get('/', (c) => {
return c.json({ const user = c.get('user')
const session = c.get('session')
const payload = {
platform: 'Helios CSMS', platform: 'Helios CSMS',
message: 'ok', message: 'ok',
}) }
if (user) {
Object.assign(payload, { user })
}
if (session) {
Object.assign(payload, { session })
}
return c.json(payload)
}) })
app.get( app.get(

View File

@@ -2,7 +2,7 @@ import { betterAuth } from 'better-auth'
import { drizzleAdapter } from 'better-auth/adapters/drizzle' import { drizzleAdapter } from 'better-auth/adapters/drizzle'
import { useDrizzle } from './db.js' import { useDrizzle } from './db.js'
import * as schema from '@/db/schema.ts' import * as schema from '@/db/schema.ts'
import { bearer, jwt, username } from 'better-auth/plugins' import { admin, bearer, jwt, username } from 'better-auth/plugins'
export const auth = betterAuth({ export const auth = betterAuth({
database: drizzleAdapter(useDrizzle(), { database: drizzleAdapter(useDrizzle(), {
@@ -12,16 +12,13 @@ export const auth = betterAuth({
}, },
}), }),
user: { user: {
additionalFields: { additionalFields: {},
role: {
type: 'string',
defaultValue: 'user',
input: false,
},
},
}, },
emailAndPassword: { emailAndPassword: {
enabled: true, enabled: true,
}, },
plugins: [username(), bearer(), jwt()], plugins: [admin(), username(), bearer(), jwt()],
advanced: {
cookiePrefix: 'helios_auth',
},
}) })