...
export const auth = betterAuth({
emailAndPassword: {
enabled: true,
requireEmailVerification: true,
sendResetPassword: async ({ user, url }) => {
void sendPasswordResetEmail(user.name, user.email, url);
},
},
emailVerification: {
autoSignInAfterVerification: true,
sendOnSignUp: true,
sendVerificationEmail: async ({ user, url }) => {
void sendVerificationEmail(user.name, user.email, url);
},
},
socialProviders: {
google: {
clientId: env.BETTER_AUTH_GOOGLE_ID,
clientSecret: env.BETTER_AUTH_GOOGLE_SECRET,
},
},
session: {
cookieCache: {
enabled: true,
maxAge: 60 * 5, // 5 minutes
},
},
user: {
additionalFields: {
balance: { type: "number", defaultValue: 0 },
role: {
type: "string",
defaultValue: "STUDENT",
validator: { input: z.enum(userRole.enumValues) },
},
},
changeEmail: {
enabled: true,
sendChangeEmailConfirmation: async ({ user, url, newEmail }) => {
void sendChangeEmailEmail(user.name, user.email, newEmail, url);
},
},
},
plugins: [
nextCookies(),
localization({
defaultLocale: "es-ES",
}),
],
advanced: {
cookiePrefix: "recursos",
},
database: drizzleAdapter(db, {
provider: "pg",
}),
hooks: {
after: createAuthMiddleware(async (ctx) => {
if (ctx.path.startsWith("/sign-up")) {
const user = ctx.context.newSession?.user ?? {
name: ctx.body.name,
email: ctx.body.email,
};
if (user) {
await sendWelcomeEmail(user.name, user.email);
}
}
}),
before: createAuthMiddleware(async (ctx) => {
if (ctx.path !== "/sign-up/email") {
return;
}
if (!ctx.body?.email.endsWith("@sanignacio.edu.uy")) {
throw new APIError("BAD_REQUEST", {
message: "Email must end with @sanignacio.edu.uy",
});
}
}),
},
});
...
export const auth = betterAuth({
emailAndPassword: {
enabled: true,
requireEmailVerification: true,
sendResetPassword: async ({ user, url }) => {
void sendPasswordResetEmail(user.name, user.email, url);
},
},
emailVerification: {
autoSignInAfterVerification: true,
sendOnSignUp: true,
sendVerificationEmail: async ({ user, url }) => {
void sendVerificationEmail(user.name, user.email, url);
},
},
socialProviders: {
google: {
clientId: env.BETTER_AUTH_GOOGLE_ID,
clientSecret: env.BETTER_AUTH_GOOGLE_SECRET,
},
},
session: {
cookieCache: {
enabled: true,
maxAge: 60 * 5, // 5 minutes
},
},
user: {
additionalFields: {
balance: { type: "number", defaultValue: 0 },
role: {
type: "string",
defaultValue: "STUDENT",
validator: { input: z.enum(userRole.enumValues) },
},
},
changeEmail: {
enabled: true,
sendChangeEmailConfirmation: async ({ user, url, newEmail }) => {
void sendChangeEmailEmail(user.name, user.email, newEmail, url);
},
},
},
plugins: [
nextCookies(),
localization({
defaultLocale: "es-ES",
}),
],
advanced: {
cookiePrefix: "recursos",
},
database: drizzleAdapter(db, {
provider: "pg",
}),
hooks: {
after: createAuthMiddleware(async (ctx) => {
if (ctx.path.startsWith("/sign-up")) {
const user = ctx.context.newSession?.user ?? {
name: ctx.body.name,
email: ctx.body.email,
};
if (user) {
await sendWelcomeEmail(user.name, user.email);
}
}
}),
before: createAuthMiddleware(async (ctx) => {
if (ctx.path !== "/sign-up/email") {
return;
}
if (!ctx.body?.email.endsWith("@sanignacio.edu.uy")) {
throw new APIError("BAD_REQUEST", {
message: "Email must end with @sanignacio.edu.uy",
});
}
}),
},
});