Added Auth
This commit is contained in:
60
src/lib/auth.ts
Normal file
60
src/lib/auth.ts
Normal 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',
|
||||
},
|
||||
})
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
Reference in New Issue
Block a user