export const auth = betterAuth({
user: {
additionalFields: {
role: {
fieldName: 'role',
type: 'string',
required: true,
input: false,
},
},
},
hooks: {
before: createAuthMiddleware(async (ctx) => {
if (ctx.path !== '/sign-up/email') {
return;
}
if (ctx.body?.registrationToken !== REGISTRATION_TOKEN) {
throw new APIError('UNAUTHORIZED', {
message: 'Incorrect registration token provided. Please contact your server admin',
});
}
}),
},
databaseHooks: {
user: {
create: {
before: async (user) => {
const adminUserExists = await db.query.usersTable.findFirst({
where: (t, { eq }) => eq(t.role, 'ADMIN'),
});
return {
data: {
...user,
role: adminUserExists ? 'USER' : 'ADMIN',
},
};
},
},
},
},
emailAndPassword: {
enabled: true,
minPasswordLength: 4,
maxPasswordLength: 100,
autoSignIn: true,
},
database: drizzleAdapter(db, {
provider: 'pg',
schema: {
user: schema.usersTable,
session: schema.sessionsTable,
account: schema.accountsTable,
verification: schema.verificationsTable,
},
}),
});
export const auth = betterAuth({
user: {
additionalFields: {
role: {
fieldName: 'role',
type: 'string',
required: true,
input: false,
},
},
},
hooks: {
before: createAuthMiddleware(async (ctx) => {
if (ctx.path !== '/sign-up/email') {
return;
}
if (ctx.body?.registrationToken !== REGISTRATION_TOKEN) {
throw new APIError('UNAUTHORIZED', {
message: 'Incorrect registration token provided. Please contact your server admin',
});
}
}),
},
databaseHooks: {
user: {
create: {
before: async (user) => {
const adminUserExists = await db.query.usersTable.findFirst({
where: (t, { eq }) => eq(t.role, 'ADMIN'),
});
return {
data: {
...user,
role: adminUserExists ? 'USER' : 'ADMIN',
},
};
},
},
},
},
emailAndPassword: {
enabled: true,
minPasswordLength: 4,
maxPasswordLength: 100,
autoSignIn: true,
},
database: drizzleAdapter(db, {
provider: 'pg',
schema: {
user: schema.usersTable,
session: schema.sessionsTable,
account: schema.accountsTable,
verification: schema.verificationsTable,
},
}),
});