Pattern: Two-step sign-in, passkeys and social login
Note for AI agents: This page is baseline guidance, not an exact prescription. Read it, tell your owner what applies to their project, and get their approval before you change anything. Your project's own rules take precedence. If something important for this project is missing here, suggest it to your owner.
For agents
- Offer as a suggestion and compare options with the owner. Not every project needs every method.
- Core ideas: add a second factor that resists phishing where you can, never make SMS the only recovery path, keep recovery codes, and alert users to sign-ins from new devices.
- Related: passwords and login, account recovery, admin permissions.
1. Problem
Passwords alone fall to leaked-password reuse and phishing. OWASP calls multi-factor authentication by far the best defense against most password-related attacks, including credential stuffing. The choice of method decides how much friction users feel and how much risk remains.
2. Options
| Method | Upside | Downside | When |
|---|---|---|---|
| Authenticator-app codes (TOTP, RFC 6238) | Works offline; no phone number needed | Codes can be phished in real time; setup friction | Default second factor for most products |
| Passkeys (WebAuthn / FIDO) | Phishing-resistant; no password to remember | Newer; recovery and device-sync design needed | Consumer apps, admin panels |
| SMS or call codes | Familiar to users | NIST treats the telephone network as a restricted authenticator; SIM swap and interception risk | Only as a fallback with eyes open |
| Email code as a factor | Everyone has email | OWASP: email is a weak factor | Low-risk step-up only |
| Magic link sign-in | No password | Depends on mailbox security and link handling | Low-risk products |
| “Sign in with Google / Apple” | No password to manage; users already trust the provider | Third-party dependency; some users lack an account; account linking rules needed | Consumer apps (see OpenID Connect Core) |
3. Recommended design
- Offer TOTP first, add passkeys where the platform supports them. Passkeys are WebAuthn credentials (W3C WebAuthn Level 3) that platform vendors sync across devices; see Google’s and Apple’s passkey developer pages and web.dev’s registration guide.
- Require the second factor for admins (see admin permissions) and offer it to everyone.
- Store the TOTP secret encrypted and reject a code that was already used (replay).
- Recovery codes: generate 8–10 single-use codes at enrolment, show them once, store only hashes. Reason: a lost phone must not lock the owner out forever.
- SMS: if you add it, treat it as the weakest option, rate-limit it and never make it the only recovery route (NIST 800-63B-4 treats the public telephone network as restricted).
- Social sign-in on mobile: use the system browser or platform sign-in component, not an embedded web view (RFC 8252: authorization requests from native apps go through external user-agents). Validate the ID token as OpenID Connect Core describes (including nonce).
- Account linking: link a social identity to an existing account only after the user proves control of the account, otherwise an attacker who creates a provider account with the same email can take it over (practitioner experience).
- Suspicious sign-in notice: when a new device or unusual place signs in, send a message (“was this you?”) with a link to review and close sessions.
- Step-up checks: ask again for the password or a factor before sensitive actions such as email change or data export.
4. Scenarios and edge cases
- Phone lost, no recovery codes: human-assisted recovery with strict identity proof (see account recovery); log and review it.
- Two devices, one passkey: synced passkeys work on both; for an unsynced one, allow registering several.
- User enrols TOTP then deletes the app: recovery codes are the way back.
- Provider account deleted: the user needs a way to set a password or another sign-in method.
5. Common mistakes
- SMS as the only second factor and the only recovery.
- Storing TOTP secrets in plain text.
- Showing recovery codes repeatedly or storing them in plain text.
- Linking social accounts by email match alone.
- Embedded web views for provider sign-in.
- Letting a code be reused within its time window.
6. How to audit
- What: a second factor exists for admins.
- How to check: sign in to the admin area with only a password.
- Evidence: the request for the second factor.
- What: a used TOTP code is rejected.
- How to check: submit the same valid code twice.
- Evidence: the second rejection.
- What: recovery codes are single-use and hashed.
- How to check: use one code twice; inspect storage.
- Evidence: rejection response; hashed values.
- What: social sign-in does not auto-link by email.
- How to check: try signing in with a provider account that shares an existing email.
- Evidence: the verification step shown.
Sources
- Multifactor Authentication Cheat Sheet — OWASP Cheat Sheet Series
- NIST SP 800-63B-4: Authentication and Authenticator Management — NIST
- Web Authentication: An API for accessing Public Key Credentials, Level 3 — W3C
- Passkeys — Google for Developers
- Passkeys Overview — Apple Developer
- Create a passkey for passwordless logins — web.dev (Google)
- RFC 6238: TOTP: Time-Based One-Time Password Algorithm — IETF / RFC Editor
- OpenID Connect Core 1.0 — OpenID Foundation
- RFC 8252: OAuth 2.0 for Native Apps — IETF / RFC Editor
- Credential Stuffing Prevention Cheat Sheet — OWASP Cheat Sheet Series
- Application Security Verification Standard (ASVS) — OWASP