Skip to content

Commit db88fe4

Browse files
committed
fix: do not send a welcome email if magic link was used on an account older than a minute ago
1 parent 7e5b354 commit db88fe4

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

docs/auth.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,10 +263,23 @@ export const auth = betterAuth({
263263
},
264264
hooks: {
265265
after: createAuthMiddleware(async (ctx) => {
266-
if (ctx.path === "/verify-email" || ctx.path === "/sign-in/social") {
266+
if (
267+
ctx.path === "/magic-link/verify" ||
268+
ctx.path === "/verify-email" ||
269+
ctx.path === "/sign-in/social"
270+
) {
267271
// After verifying email, send them a welcome email
268272
const newSession = ctx.context.newSession;
269273
if (newSession) {
274+
const oneMinuteAgo = new Date(Date.now() - 60 * 1000);
275+
if (
276+
ctx.path === "/magic-link/verify" &&
277+
newSession.user.createdAt < oneMinuteAgo
278+
) {
279+
// magic link is for an account that was created more than a minute ago, so just a normal sign in
280+
// no need to send welcome email
281+
return false;
282+
}
270283
await sendEmail({
271284
to: newSession.user.email,
272285
template: "welcome",

0 commit comments

Comments
 (0)