77 lines
2.3 KiB
TypeScript
77 lines
2.3 KiB
TypeScript
import { betterAuth } from 'better-auth';
|
|
import { prismaAdapter } from '@better-auth/prisma-adapter';
|
|
import prisma from './prisma.js';
|
|
import EmailSender from './email-sender.js';
|
|
import { authTransformResponse } from './auth-plugins/auth-response.js';
|
|
|
|
export const auth = betterAuth({
|
|
appName: process.env.APP_NAME ?? 'app',
|
|
basePath: '/api/auth',
|
|
plugins: [
|
|
// authTransformResponse()
|
|
],
|
|
database: prismaAdapter(prisma, {
|
|
provider: 'postgresql',
|
|
}),
|
|
advanced: {
|
|
disableOriginCheck: true,
|
|
disableCSRFCheck: true,
|
|
database: {
|
|
joins: true,
|
|
},
|
|
},
|
|
hooks: {},
|
|
secret: process.env.BETTER_AUTH_SECRET,
|
|
baseURL: process.env.BETTER_AUTH_URL ?? 'http://localhost:3000',
|
|
emailAndPassword: {
|
|
enabled: true,
|
|
autoSignIn: false,
|
|
requireEmailVerification: false,
|
|
onExistingUserSignUp: async ({ user }, request) => {
|
|
console.log('onExistingUserSignUp', { user });
|
|
|
|
void EmailSender.getTransporter().sendMail({
|
|
to: user.email,
|
|
subject: 'Sign-up attempt with your email',
|
|
text: 'Someone tried to create an account using your email address. If this was you, try signing in instead. If not, you can safely ignore this email.',
|
|
});
|
|
},
|
|
sendResetPassword: async ({ user, url, token }, request) => {
|
|
console.log('sendResetPassword', { user, url, token });
|
|
void EmailSender.getTransporter().sendMail({
|
|
to: user.email,
|
|
subject: 'Reset your password',
|
|
text: `Click the link to reset your password: ${url}`,
|
|
});
|
|
},
|
|
onPasswordReset: async ({ user }, request) => {
|
|
// your logic here
|
|
console.log('onPasswordReset', { user });
|
|
void EmailSender.getTransporter().sendMail({
|
|
to: user.email,
|
|
subject: 'Reset your password result',
|
|
text: `Password for user ${user.email} has been reset.`,
|
|
});
|
|
},
|
|
},
|
|
emailVerification: {
|
|
sendOnSignIn: true,
|
|
autoSignInAfterVerification: true,
|
|
sendVerificationEmail: async ({ user, url }) => {
|
|
console.log('emailVerification', { user, url });
|
|
void EmailSender.getTransporter().sendMail({
|
|
to: user.email,
|
|
subject: 'Verify your email address',
|
|
text: `Click the link to verify your email: ${url}`,
|
|
});
|
|
},
|
|
},
|
|
session: {
|
|
freshAge: 0,
|
|
cookieCache: {
|
|
enabled: true,
|
|
maxAge: 30,
|
|
},
|
|
},
|
|
});
|