From 66a32a03370e4f620c9f1cf573a97fb77147e199 Mon Sep 17 00:00:00 2001 From: Tomas Francisco <4301103+tomasfrancisco@users.noreply.github.com> Date: Wed, 15 Jan 2025 15:40:09 +0100 Subject: [PATCH 1/3] fix: schedule emails on user first sign in --- .../app/auth/_actions/verify-otp.action.ts | 76 ++++++++++--------- 1 file changed, 40 insertions(+), 36 deletions(-) diff --git a/apps/engine/src/app/auth/_actions/verify-otp.action.ts b/apps/engine/src/app/auth/_actions/verify-otp.action.ts index 566f482..a61e3ff 100644 --- a/apps/engine/src/app/auth/_actions/verify-otp.action.ts +++ b/apps/engine/src/app/auth/_actions/verify-otp.action.ts @@ -26,47 +26,51 @@ export const verifyOtpAction = publicAction type: 'email', }); - if (!error) { - if ( - data.user?.id && - data.user.email_confirmed_at && - new Date(data.user.email_confirmed_at).getTime() < - new Date().getTime() + 1000 * 60 * 1 // 1 minute - ) { - const result = await ctx.authClient - .from('accounts') - .select('id') - .eq('user_id', data.user.id) - .single(); + if (error) { + return { + error: error.message, + ok: false, + }; + } - if (result.error) { - return { - error: result.error.message, - ok: false, - }; - } + // Check if this is a fresh email confirmation (within last minute) + const isFirstTimeSignIn = + data.user?.last_sign_in_at && + data.user.email_confirmed_at && + new Date(data.user.email_confirmed_at).getTime() + 5 * 60 * 1000 > // within five minutes + new Date(data.user.last_sign_in_at).getTime(); - await sendEmail({ - accountId: result.data.id, - subject: 'Welcome to DS Pro', - template: { - key: 'welcome', - props: { - staticPathUrl: `${config.pageUrl}/static/email`, - }, - }, - scheduledAt: 'in 5 minutes', - }); + // Check if user has any previous activity in accounts table + if (data.user && isFirstTimeSignIn) { + const result = await ctx.authClient + .from('accounts') + .select('id') + .eq('user_id', data.user.id) + .single(); - await scheduleOnboardingEmails(result.data.id); + if (result.error) { return { - ok: true, + error: 'Error with the user account', + ok: false, }; } - } - return { - error: error?.message ?? 'Error verifying OTP', - ok: false, - }; + await sendEmail({ + accountId: result.data.id, + subject: 'Welcome to DS Pro', + template: { + key: 'welcome', + props: { + staticPathUrl: `${config.pageUrl}/static/email`, + }, + }, + scheduledAt: 'in 5 minutes', + }); + + await scheduleOnboardingEmails(result.data.id); + + return { + ok: true, + }; + } }); From 607e27a6a8275e5c4f4e63e9049acbca89b0d247 Mon Sep 17 00:00:00 2001 From: Tomas Francisco <4301103+tomasfrancisco@users.noreply.github.com> Date: Wed, 15 Jan 2025 15:40:44 +0100 Subject: [PATCH 2/3] chore: update comment --- apps/engine/src/app/auth/_actions/verify-otp.action.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/engine/src/app/auth/_actions/verify-otp.action.ts b/apps/engine/src/app/auth/_actions/verify-otp.action.ts index a61e3ff..34f4251 100644 --- a/apps/engine/src/app/auth/_actions/verify-otp.action.ts +++ b/apps/engine/src/app/auth/_actions/verify-otp.action.ts @@ -33,7 +33,7 @@ export const verifyOtpAction = publicAction }; } - // Check if this is a fresh email confirmation (within last minute) + // Check if this is a fresh email confirmation (within last 5 minute) const isFirstTimeSignIn = data.user?.last_sign_in_at && data.user.email_confirmed_at && From e3a98be21261b46f96a39bdf77c7d3dbe098cb63 Mon Sep 17 00:00:00 2001 From: Tomas Francisco <4301103+tomasfrancisco@users.noreply.github.com> Date: Wed, 15 Jan 2025 15:41:04 +0100 Subject: [PATCH 3/3] chore: update comments --- apps/engine/src/app/auth/_actions/verify-otp.action.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/apps/engine/src/app/auth/_actions/verify-otp.action.ts b/apps/engine/src/app/auth/_actions/verify-otp.action.ts index 34f4251..c5a4819 100644 --- a/apps/engine/src/app/auth/_actions/verify-otp.action.ts +++ b/apps/engine/src/app/auth/_actions/verify-otp.action.ts @@ -40,7 +40,6 @@ export const verifyOtpAction = publicAction new Date(data.user.email_confirmed_at).getTime() + 5 * 60 * 1000 > // within five minutes new Date(data.user.last_sign_in_at).getTime(); - // Check if user has any previous activity in accounts table if (data.user && isFirstTimeSignIn) { const result = await ctx.authClient .from('accounts')