Overview

14 Passwordless login: Using Magic links and OTPs

Passwordless authentication removes the friction and fragility of memorized secrets by replacing passwords with sign-in actions tied to possession and intent. This chapter explains two widely deployed approaches—magic links and one-time passwords (OTPs)—showing how they improve usability while stressing that security depends on careful implementation and user awareness. It highlights common pitfalls such as account enumeration, phishing, token reuse, and weak delivery channels, and frames passwordless as part of a broader journey toward stronger, phishing-resistant methods like WebAuthn.

With magic links, a user submits an email address and receives a short‑lived, cryptographically signed token embedded in a link. The application must prevent email enumeration with generic responses; generate tokens with strong signing, explicit expiration, and a nonce; store and invalidate tokens on first use to stop replay; throttle requests; and monitor for anomalies. Delivery should be clearly branded, and the token should not appear as plaintext to avoid leakage via previews, forwarding, or logs. On click, the app verifies signature, expiry, and one‑time use, then establishes a session. Because email compromise and phishing remain real threats, domain checks, user education, and optional MFA can meaningfully raise the bar.

OTPs follow a similar pattern but require users to enter a short code that is randomly generated, time‑bound, and single‑use, then validated for correctness and expiry before issuing a session. Their benefits include being ephemeral, not requiring memory, and working across channels (SMS, email, or authenticator apps), making them a strong fit for MFA as “something you have.” Risks include SMS interception (e.g., SIM swap), phishing, and brute force—mitigated by using cryptographically secure generation, short lifetimes, rate limiting on requests and entry attempts, monitoring for suspicious activity, favoring app‑based TOTPs over SMS, and providing safe fallbacks. Compared with magic links, OTPs trade a little convenience for flexibility; both can deliver secure, user‑friendly logins when paired with strict expiry, one‑time semantics, throttling, auditing, and phishing defenses.

Authenticating through magic links. Upon the user’s request, the app generates a unique link and sends it to the user by email. The user clicks on the link to authenticate.
A malicious individual might send a fake but realistically looking email between steps 3 and 4 of the process. An inattentive user could be fooled into clicking the wrong link.
Security is only as strong as its weakest link. You can have the best defenses, but if you leave the key under the rug… well, good luck!
The OTP authentication process. The app generates a random OTP and sends it to the user. The user uses the received OTP to authenticate.

Summary

  • Passwordless authentication improves security and convenience by eliminating weak or reused passwords. This chapter explored Magic Links, OTPs, Biometric Authentication, and Hardware Security Keys as alternatives. Each method has its advantages and risks.
  • Magic links allow users to log in via a one-time link sent to their email. However, they are vulnerable to phishing and email compromise. Using short-lived, cryptographically signed tokens and preventing email enumeration helps mitigate risks.
  • OTPs provide temporary numeric codes via SMS, email, or authenticator apps. They are susceptible to SIM-swapping, phishing, and brute-force attacks. Using app-based OTPs (Google Authenticator, Authy) and limiting OTP requests increases security.
  • Passwordless authentication reduces phishing, credential leaks, and login friction, but no single method fits all use cases. Choosing the right approach depends on security needs, usability, and risk factors.
  • Proper implementation is key—even the most secure authentication method can be exploited if configured poorly. Following best practices, encryption standards, and secure handling of authentication data ensures a stronger, safer login experience.

FAQ

What is a magic link and how does it authenticate users?Users enter their email, the app generates a signed, short‑lived token embedded in a URL, and emails the link. When clicked, the app verifies the token’s signature, expiration, and that it hasn’t been used, then creates a session and invalidates the token.
Why shouldn’t my app reveal whether an email address exists?Disclosing account existence enables email enumeration. Attackers can confirm valid users and launch targeted phishing, credential stuffing, or brute‑force attempts. Use generic responses like “If an account exists, we’ve sent a link.”
How do I generate a secure magic link token?Create a cryptographically signed token (e.g., JWT or HMAC) that includes user ID/email, an expiration (5–15 minutes), and a nonce to prevent replay. Deliver the link over HTTPS.
Why store magic link tokens in a database or cache?To mark tokens as used (one‑time use), support revocation, throttle requests, detect abuse (e.g., many tokens for one email), and enable auditing/alerts for unusual activity.
Why shouldn’t the tokenized URL appear in plaintext in the email?Visible tokens can leak via previews, notifications, forwarding, or logging by email/security tools. Keep the token only in the clickable href and avoid showing it as visible text.
How can I reduce phishing risk in magic link flows?Use clear branding and familiar templates, verify domains/links, keep links short‑lived and one‑time, warn users about suspicious activity, and consider adding MFA before finalizing login.
How do One-Time Passwords (OTPs) work and what are their benefits?The app generates a cryptographically random 6–8 digit code with a short expiration. It’s delivered via SMS, email, or an authenticator app, entered by the user, and validated for correctness, expiry, and single use. Benefits: time‑limited, single‑use, no static secret to remember or reuse.
What are common OTP risks and how do I mitigate them?Risks: SMS interception (SIM swapping, SS7), phishing, and brute‑force guessing. Mitigations: prefer app‑based OTPs, enforce rate limits on requests and entries, short expirations, hash and delete OTPs after use, and monitor for suspicious patterns.
How do OTPs and magic links fit into multi-factor authentication (MFA)?OTPs often serve as “something you have” alongside a password or biometric. Magic links are typically single‑factor; for higher‑risk actions, pair them with an additional factor.
Should I choose magic links or OTPs for my app?Choose magic links for a smooth, passwordless experience when email is reliable. Choose OTPs for MFA, TOTP app support, or when email is unreliable. Both require phishing protections, short expirations, one‑time use, and rate limiting.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Software Security for Developers ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Software Security for Developers ebook for free