60 lines
1.7 KiB
TypeScript
60 lines
1.7 KiB
TypeScript
import { dash } from '@better-auth/infra';
|
|
import { betterAuth } from 'better-auth';
|
|
import { prismaAdapter } from 'better-auth/adapters/prisma';
|
|
import { openAPI } from 'better-auth/plugins';
|
|
import { sendMail } from './mailer';
|
|
import { db } from './prisma';
|
|
|
|
export const auth = betterAuth({
|
|
database: prismaAdapter(db, {
|
|
provider: 'postgresql',
|
|
}),
|
|
secret: process.env.BETTER_AUTH_SECRET,
|
|
baseURL: process.env.BETTER_AUTH_URL,
|
|
session: {
|
|
cookieCache: {
|
|
enabled: false,
|
|
},
|
|
},
|
|
emailAndPassword: {
|
|
enabled: true,
|
|
},
|
|
user: {
|
|
additionalFields: {
|
|
phone: {
|
|
type: 'string',
|
|
required: false,
|
|
},
|
|
},
|
|
},
|
|
emailVerification: {
|
|
sendOnSignUp: true,
|
|
autoSignInAfterVerification: true,
|
|
sendVerificationEmail: async ({ user, url }) => {
|
|
const verificationUrl = new URL(url);
|
|
const appUrl = process.env.APP_BASE_URL ?? 'http://localhost:5173';
|
|
verificationUrl.searchParams.set('callbackURL', appUrl);
|
|
await sendMail({
|
|
to: user.email,
|
|
subject: 'Verificá tu email en Playzer',
|
|
html: `Hacé click para verificar tu email: <a href="${verificationUrl.toString()}">${verificationUrl.toString()}</a>`,
|
|
text: `Hacé click para verificar tu email: ${verificationUrl.toString()}`,
|
|
});
|
|
},
|
|
},
|
|
socialProviders: {
|
|
google: {
|
|
clientId: process.env.GOOGLE_CLIENT_ID!,
|
|
clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
|
|
},
|
|
},
|
|
trustedOrigins: [
|
|
process.env.APP_BASE_URL ?? 'http://localhost:5173',
|
|
process.env.BETTER_AUTH_URL ?? 'http://localhost:3000',
|
|
],
|
|
plugins: [openAPI(), dash()],
|
|
});
|
|
|
|
export type Session = typeof auth.$Infer.Session.session;
|
|
export type User = typeof auth.$Infer.Session.user;
|