feat(csms): add deployment script and database schema for user authentication
This commit is contained in:
27
apps/csms/src/lib/auth.ts
Normal file
27
apps/csms/src/lib/auth.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { betterAuth } from 'better-auth'
|
||||
import { drizzleAdapter } from 'better-auth/adapters/drizzle'
|
||||
import { useDrizzle } from './db.js'
|
||||
import * as schema from '@/db/schema.ts'
|
||||
import { bearer, jwt, username } from 'better-auth/plugins'
|
||||
|
||||
export const auth = betterAuth({
|
||||
database: drizzleAdapter(useDrizzle(), {
|
||||
provider: 'pg',
|
||||
schema: {
|
||||
...schema,
|
||||
},
|
||||
}),
|
||||
user: {
|
||||
additionalFields: {
|
||||
role: {
|
||||
type: 'string',
|
||||
defaultValue: 'user',
|
||||
input: false,
|
||||
},
|
||||
},
|
||||
},
|
||||
emailAndPassword: {
|
||||
enabled: true,
|
||||
},
|
||||
plugins: [username(), bearer(), jwt()],
|
||||
})
|
||||
24
apps/csms/src/lib/db.ts
Normal file
24
apps/csms/src/lib/db.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { drizzle } from 'drizzle-orm/node-postgres'
|
||||
import { Pool } from 'pg'
|
||||
import * as schema from '@/db/schema.js'
|
||||
|
||||
let pgPoolInstance: Pool | null = null
|
||||
let drizzleInstance: ReturnType<typeof drizzle> | null = null
|
||||
|
||||
export const useDrizzle = () => {
|
||||
if (!pgPoolInstance || !drizzleInstance) {
|
||||
pgPoolInstance = new Pool({
|
||||
connectionString: process.env.DATABASE_CONNECTION_STRING,
|
||||
})
|
||||
drizzleInstance = drizzle({ client: pgPoolInstance, schema })
|
||||
}
|
||||
return drizzleInstance
|
||||
}
|
||||
|
||||
export const closeDrizzle = () => {
|
||||
if (pgPoolInstance) {
|
||||
pgPoolInstance.end()
|
||||
pgPoolInstance = null
|
||||
drizzleInstance = null
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user