export const auth = betterAuth({
database: prismaAdapter(prisma, {
provider: 'postgresql',
}),
session: {
expiresIn: ONE_YEAR_IN_SECONDS,
},
emailAndPassword: {
enabled: true,
requireEmailVerification: true,
sendResetPassword: async ({ user, url }) => {
const email = getResetPasswordEmail(url);
await sendEmail(user.email, 'Reset your password', email);
},
},
emailVerification: {
sendVerificationEmail: async ({ user, url }) => {
const email = getSignUpVerificationEmail(url);
await sendEmail(user.email, 'Verify your email address', email);
},
expiresAt: ONE_DAY_IN_SECONDS,
sendOnSignUp: true,
autoSignInAfterVerification: true,
},
plugins: [
customSession(async ({ user, session }) => {
const roles = await getRoles();
return {
session,
user: {
...user,
roles: roles.map((role: { name: string }) => role.name),
},
};
}),
],
hooks: {
after: createAuthMiddleware(async (ctx) => {
if (ctx.path.startsWith('/verify-email')) {
const newSession = ctx.context.newSession;
...
}
}),
},
});
export const auth = betterAuth({
database: prismaAdapter(prisma, {
provider: 'postgresql',
}),
session: {
expiresIn: ONE_YEAR_IN_SECONDS,
},
emailAndPassword: {
enabled: true,
requireEmailVerification: true,
sendResetPassword: async ({ user, url }) => {
const email = getResetPasswordEmail(url);
await sendEmail(user.email, 'Reset your password', email);
},
},
emailVerification: {
sendVerificationEmail: async ({ user, url }) => {
const email = getSignUpVerificationEmail(url);
await sendEmail(user.email, 'Verify your email address', email);
},
expiresAt: ONE_DAY_IN_SECONDS,
sendOnSignUp: true,
autoSignInAfterVerification: true,
},
plugins: [
customSession(async ({ user, session }) => {
const roles = await getRoles();
return {
session,
user: {
...user,
roles: roles.map((role: { name: string }) => role.name),
},
};
}),
],
hooks: {
after: createAuthMiddleware(async (ctx) => {
if (ctx.path.startsWith('/verify-email')) {
const newSession = ctx.context.newSession;
...
}
}),
},
});