Files
formbricks/apps/web/modules/auth/verify-email-change/actions.ts
T
2025-10-20 14:28:14 +00:00

36 lines
1.2 KiB
TypeScript

"use server";
import { z } from "zod";
import { verifyEmailChangeToken } from "@/lib/jwt";
import { actionClient } from "@/lib/utils/action-client";
import { ActionClientCtx } from "@/lib/utils/action-client/types/context";
import { updateBrevoCustomer } from "@/modules/auth/lib/brevo";
import { getUser, updateUser } from "@/modules/auth/lib/user";
import { withAuditLogging } from "@/modules/ee/audit-logs/lib/handler";
export const verifyEmailChangeAction = actionClient.schema(z.object({ token: z.string() })).action(
withAuditLogging(
"updated",
"user",
async ({ ctx, parsedInput }: { ctx: ActionClientCtx; parsedInput: Record<string, any> }) => {
const { id, email } = await verifyEmailChangeToken(parsedInput.token);
if (!email) {
throw new Error("Email not found in token");
}
const oldObject = await getUser(id);
const user = await updateUser(id, { email, emailVerified: new Date() });
if (!user) {
throw new Error("User not found or email update failed");
}
ctx.auditLoggingCtx.userId = id;
ctx.auditLoggingCtx.oldObject = oldObject;
ctx.auditLoggingCtx.newObject = user;
await updateBrevoCustomer({ id: user.id, email: user.email });
return user;
}
)
);