Skip to content
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