Adding reset password feature.

This commit is contained in:
charlesgauthereau
2025-11-22 17:20:52 +01:00
parent aabdea06a9
commit fe5d8bae1b
29 changed files with 2596 additions and 290 deletions
+15 -2
View File
@@ -9,6 +9,9 @@ import {ac, admin, orgAdmin, orgMember, orgOwner, pending, superadmin, user} fro
import {headers} from "next/headers";
import {count, eq} from "drizzle-orm";
import {MemberWithUser, OrganizationWithMembersAndUsers} from "@/db/schema/03_organization";
import {sendEmail} from "@/lib/email/email-helper";
import {render} from "@react-email/render";
import EmailResetPassword from "@/components/emails/email-reset-password";
export const auth = betterAuth({
database: drizzleAdapter(db, {
@@ -19,15 +22,25 @@ export const auth = betterAuth({
emailAndPassword: {
enabled: true,
requireEmailVerification: false,
/*async sendResetPassword(data, request) {
// Send an email to the user with a link to reset their password
sendResetPassword: async ({user, url, token}, request) => {
await sendEmail({
to: user.email,
subject: "Reset your password",
html: await render(EmailResetPassword({url: url})),
});
},
onPasswordReset: async ({user}, request) => {
console.log(`Password for user ${user.email} has been reset.`);
},
/*
async sendVerificationEmail(data, request) {
// Send an email to the user with a link to verify their email
},
async verifyEmail(data, request) {
// Verify the email address
},*/
},
socialProviders: {
google: {
+94
View File
@@ -0,0 +1,94 @@
"use server";
import {db} from "@/db";
import {eq} from "drizzle-orm";
import nodemailer from "nodemailer";
import * as drizzleDb from "@/db";
type Payload = {
to: string;
from?: string;
subject: string;
html: any;
};
type Server = {
host: string;
port: number;
user: string;
pass: string;
from: string;
};
type EmailCustomProps = {
data: Payload;
server: Server;
};
type EmailMassProps = {
data: Payload;
servers: Server[];
};
export const sendEmail = async (data: Payload) => {
const settings = await db
.select()
.from(drizzleDb.schemas.setting)
.where(eq(drizzleDb.schemas.setting.name, "system"))
.then((res) => res[0]);
if (!settings) {
throw new Error("SMTP system settings not found.");
}
const transporter = nodemailer.createTransport({
pool: true,
host: settings.smtpHost ?? "",
port: parseInt(settings.smtpPort ?? "587"),
secure: true,
auth: {
user: settings.smtpUser ?? "",
pass: settings.smtpPassword ?? "",
},
});
return await transporter.sendMail({
...data,
from: settings.smtpFrom ?? undefined,
});
};
export const sendCustomEmail = async (data: EmailCustomProps) => {
const transporter = nodemailer.createTransport({
secure: true,
replyTo: data.server.user,
host: data.server.host,
port: data.server.port,
auth: {
user: data.server.user,
pass: data.server.pass,
},
});
return await transporter.sendMail({
...data.data,
});
};
export const sendMassEmail = async (data: EmailMassProps) => {
for (const server of data.servers) {
const transporter = nodemailer.createTransport({
secure: true,
host: server.host,
port: server.port,
auth: {
user: server.user,
pass: server.pass,
},
});
return await transporter.sendMail({
...data.data,
});
}
};