Skip to content

Commit db35aaf

Browse files
fix: schedule emails on user first sign in (#82)
1 parent fbc892a commit db35aaf

File tree

1 file changed

+39
-36
lines changed

1 file changed

+39
-36
lines changed

apps/engine/src/app/auth/_actions/verify-otp.action.ts

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -26,47 +26,50 @@ export const verifyOtpAction = publicAction
2626
type: 'email',
2727
});
2828

29-
if (!error) {
30-
if (
31-
data.user?.id &&
32-
data.user.email_confirmed_at &&
33-
new Date(data.user.email_confirmed_at).getTime() <
34-
new Date().getTime() + 1000 * 60 * 1 // 1 minute
35-
) {
36-
const result = await ctx.authClient
37-
.from('accounts')
38-
.select('id')
39-
.eq('user_id', data.user.id)
40-
.single();
29+
if (error) {
30+
return {
31+
error: error.message,
32+
ok: false,
33+
};
34+
}
4135

42-
if (result.error) {
43-
return {
44-
error: result.error.message,
45-
ok: false,
46-
};
47-
}
36+
// Check if this is a fresh email confirmation (within last 5 minute)
37+
const isFirstTimeSignIn =
38+
data.user?.last_sign_in_at &&
39+
data.user.email_confirmed_at &&
40+
new Date(data.user.email_confirmed_at).getTime() + 5 * 60 * 1000 > // within five minutes
41+
new Date(data.user.last_sign_in_at).getTime();
4842

49-
await sendEmail({
50-
accountId: result.data.id,
51-
subject: 'Welcome to DS Pro',
52-
template: {
53-
key: 'welcome',
54-
props: {
55-
staticPathUrl: `${config.pageUrl}/static/email`,
56-
},
57-
},
58-
scheduledAt: 'in 5 minutes',
59-
});
43+
if (data.user && isFirstTimeSignIn) {
44+
const result = await ctx.authClient
45+
.from('accounts')
46+
.select('id')
47+
.eq('user_id', data.user.id)
48+
.single();
6049

61-
await scheduleOnboardingEmails(result.data.id);
50+
if (result.error) {
6251
return {
63-
ok: true,
52+
error: 'Error with the user account',
53+
ok: false,
6454
};
6555
}
66-
}
6756

68-
return {
69-
error: error?.message ?? 'Error verifying OTP',
70-
ok: false,
71-
};
57+
await sendEmail({
58+
accountId: result.data.id,
59+
subject: 'Welcome to DS Pro',
60+
template: {
61+
key: 'welcome',
62+
props: {
63+
staticPathUrl: `${config.pageUrl}/static/email`,
64+
},
65+
},
66+
scheduledAt: 'in 5 minutes',
67+
});
68+
69+
await scheduleOnboardingEmails(result.data.id);
70+
71+
return {
72+
ok: true,
73+
};
74+
}
7275
});

0 commit comments

Comments
 (0)