Prompt
How do I support email signup and passwordless login in one flow?
Latest observation
A good way to support email signup + passwordless login in one flow is to treat them as the same user journey:
- User enters email
- You send a magic link or one-time code
- If the email is new, you create the account automatically
- If the email already exists, you log them in
- After verification, you optionally collect profile details
Recommended flow
1) Ask only for email first
Keep the first step simple:
- Email input
- Button: “Continue”
At this point, don’t ask for a password.
2) Send a verification link or code
After submission:
- Generate a short-lived token or one-time code
- Send it to the email
- Tell the user: “Check your inbox”
You can use either:
- Magic link: best UX
- 6-digit code: better for cross-device/manual entry
3) On link/code verification, determine account state
When the user proves they own the email:
- If an account with that email already exists:
- Sign them in
- If no account exists:
- Create a new account
- Mark email as verified
- Sign them in immediately
That’s the core of “signup and login in one flow.”
Best-practice behavior
Use email as the primary identifier
Since there’s no password, email becomes the login key.
Don’t reveal whether an email exists before verification
For security and privacy, avoid responses like:
- “No account found”
- “We found your account”
Instead, always say something like:
- “If an account exists, we’ve sent a sign-in link.”
This helps prevent account enumeration.
Make signup automatic after verification
If the email is new, create the user record at the moment the email is verified.
Typical fields:
idemailemail_verified_atcreated_atlast_login_at
Allow setting a password later, optionally
You can stay passwordless forever, or let users add a password later under account settings if needed.
Example flow
New user
- Enters
alice@example.com - Receives magic link
- Clicks link
- System sees no existing user
- Creates account
- Logs them in
- Optionally asks for name/team preferences
Existing user
- Enters
alice@example.com - Receives magic link
- Clicks link
- System finds account
- Logs them in
Same UX, different backend outcome.
Implementation pattern
Backend endpoints
You usually need two endpoints:
-
POST /auth/start- input: email
- action: generate and send token/code
-
POST /auth/verify- input: token/code
- action: validate token, create account if needed, issue session
Token design
Use tokens that are:
- Random
- Single-use
- Short-lived, e.g. 10–15 minutes
- Stored hashed in the database if possible
Session creation
After successful verification:
- Create a server session cookie, or
- Issue a JWT/access token
Common UX details
- Show a resend button after ~30 seconds
- Expire tokens after a short time
- Let users change email if they mistyped it
- Support “open link on same device” and “enter code manually” if possible
- Show a friendly message if the token expired
Edge cases to handle
- Email typo: allow editing before verification
- Multiple requests: invalidate older tokens when a new one is sent
- Duplicate accounts: ensure email is unique in your user table
- Unverified existing account: resend verification or re-send login link
- Rate limiting: prevent abuse of email sending
If you want the simplest possible version
Use this exact policy:
- User enters email
- Send magic link
- If verified email matches an existing user, log in
- Otherwise, create user and log them in
- Never require a password unless the user chooses to add one later
If you want, I can also give you:
- a database schema,
- API pseudocode, or
- an implementation example for Next.js, Django, Rails, or Firebase.