added email auth

This commit is contained in:
Sanjib Kumar Sen 2025-01-11 21:33:58 +06:00
parent 8cb31194d7
commit 0f29052fea
2 changed files with 38 additions and 1 deletions

View file

@ -3,10 +3,10 @@ import { drizzleAdapter } from "better-auth/adapters/drizzle";
import { db } from "../../db/index"; import { db } from "../../db/index";
import { openAPI } from "better-auth/plugins" import { openAPI } from "better-auth/plugins"
import { user, account, verification, session } from "../../db/schema"; import { user, account, verification, session } from "../../db/schema";
import { sendMail } from "../mail";
export const auth = betterAuth({ export const auth = betterAuth({
database: drizzleAdapter(db, { database: drizzleAdapter(db, {
// We're using Drizzle as our database
provider: "pg", provider: "pg",
schema: { schema: {
user: user, user: user,
@ -17,6 +17,24 @@ export const auth = betterAuth({
}), }),
emailAndPassword: { emailAndPassword: {
enabled: true, // If you want to use email and password auth enabled: true, // If you want to use email and password auth
requireEmailVerification: false,
sendResetPassword: async ({ user, url }, request) => {
await sendMail({
to: user.email,
subject: "Reset your password",
text: `Click the link to reset your password: ${url}`,
});
},
},
emailVerification: {
sendVerificationEmail: async ({ user, url, token }, request) => {
await sendMail({
to: user.email,
subject: "Verify your email address",
text: `Click the link to verify your email: ${url}`,
});
},
}, },
plugins: [ plugins: [
openAPI({ openAPI({

19
src/lib/mail.ts Normal file
View file

@ -0,0 +1,19 @@
import nodemailer from 'nodemailer'
export async function sendMail({ to, subject, text }: { to: string, subject: string, text: string }) {
const transporter = nodemailer.createTransport({
host: process.env.SMTP_HOST!,
port: +process.env.SMTP_PORT!,
auth: {
user: process.env.SMTP_USER!,
pass: process.env.SMTP_PASSWORD!,
}
})
await transporter.sendMail({
from: process.env.SMTP_FROM!,
to,
subject,
text,
})
}