Email Authentication — Email-Based Verification and Login Patterns
DodaTech
Updated 2026-06-28
1 min read
In this tutorial, you'll learn about Email Authentication. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.
Email-based authentication provides a familiar, low-friction auth method using the user's email address as the primary identifier.
// Email verification flow
async function sendVerificationEmail(user) {
const token = crypto.randomBytes(32).toString('hex');
const expires = Date.now() + 24 * 60 * 60 * 1000;
await redis.setex(`verify_email:${token}`, 86400, JSON.stringify({
userId: user.id,
newEmail: user.email,
type: 'verification'
}));
const verifyUrl = `https://scanapp.com/verify-email?token=${token}`;
await emailService.send({
to: user.email,
subject: 'Verify your ScanApp email',
html: `
<h2>Welcome to ScanApp!</h2>
<p>Please verify your email by clicking the link below:</p>
<a href="${verifyUrl}" style="padding:12px 24px; background:#2d6a9f; color:white;">
Verify Email
</a>
<p>Link expires in 24 hours.</p>
`
});
}
// Magic link login
async function sendMagicLink(email) {
const token = crypto.randomBytes(32).toString('hex');
await redis.setex(`magic_link:${token}`, 900, email);
const loginUrl = `https://scanapp.com/auth/magic-link?token=${token}`;
await emailService.send({
to: email,
subject: 'Sign in to ScanApp',
text: `Click to sign in: ${loginUrl}. Expires in 15 minutes.`
});
}
// Email OTP
async function sendEmailOTP(email) {
const code = Math.floor(100000 + Math.random() * 900000).toString();
await redis.setex(`email_otp:${email}`, 300, code);
await emailService.send({
to: email,
subject: 'Your ScanApp verification code',
text: `Your verification code is: ${code}. Valid for 5 minutes.`
});
}
Email-based authentication is universally accessible and provides a reliable fallback for other auth methods.
← Previous
Push Notification Authentication — 2FA via Push Notifications
Next →
Hardware Security Keys — FIDO2 and WebAuthn Hardware Authentication
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro