Added Auth

This commit is contained in:
2025-12-22 10:47:15 +07:00
parent 2c244b77fb
commit fa029365d0
79 changed files with 19643 additions and 2830 deletions

60
src/lib/auth.ts Normal file
View File

@@ -0,0 +1,60 @@
import { betterAuth } from 'better-auth'
import { admin as adminPlugin, organization } from 'better-auth/plugins'
import { prismaAdapter } from 'better-auth/adapters/prisma'
import { prisma } from '@/db'
import { ac, admin, user } from '@/lib/auth/permissions'
import {
acOrg,
adminOrg,
member,
owner,
} from '@/lib/auth/organization-permissions'
export const auth = betterAuth({
database: prismaAdapter(prisma, {
provider: 'postgresql',
}),
experimental: { joins: true },
emailAndPassword: {
enabled: true,
requireEmailVerification: false,
},
trustedOrigins: ['http://localhost:3001'],
plugins: [
adminPlugin({
ac,
roles: { admin, user },
defaultRole: 'user',
}),
organization({
ac: acOrg,
roles: { owner, admin: adminOrg, member },
schema: {
organization: {
additionalFields: {
color: {
type: 'string',
defaultValue: '#000000',
},
},
},
},
}),
],
databaseHooks: {
user: {
create: {
after: async (user) => {
await auth.api.createOrganization({
body: {
name: `${user.name || 'User'}'s Organization`,
slug: `${user.name?.toLowerCase().replace(/\s+/g, '-')}-${Date.now()}`,
userId: user.id,
color: '#000000',
},
})
},
},
},
},
})