Skip to content

fix: schedule emails on user first sign in #82

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 15, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 39 additions & 36 deletions apps/engine/src/app/auth/_actions/verify-otp.action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,47 +26,50 @@ 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 5 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',
});
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,
};
}
});
Loading