Repository navigation
Sign in by email with a link and a code, alongside Google - #53
Merged
Merged
Conversation
The sign-in page keeps "Continue with Google" and adds an email form. One
message carries both a "Sign in" link and a six-digit code, and either one
signs the reader in. A first sign-in creates the account and sends the usual
welcome email.
The service is ours (`apps/accounts/email_login.py`), not allauth's
login-by-code: that flow is bound to the browser that asked, so its link
could not finish on a phone a sign-in started on a laptop. It ends in the
same JWT pair as the Google exchange, so refresh and rotation are unchanged.
- `POST /auth/email-login` answers 204 for every address, sends nothing on
a repeat within 60 seconds, and mails in the request, never queued: the
mail carries the raw code and link.
- `POST /auth/email-login/confirm` takes `{email, code}` or `{token}`. The
code and link share one row, stored as SHA-256 only, so using one spends
both. A row dies after 15 minutes, 5 wrong codes, or a newer request.
- Both endpoints are throttled per IP (`EMAIL_LOGIN_REQUEST_RATE`,
`EMAIL_LOGIN_CONFIRM_RATE`).
- Signing in records a verified allauth `EmailAddress`, so a later Google
sign-in with the same address lands on the same account.
- Frontend: a next-auth `Credentials` provider with id `email`. The code is
typed on `/auth/signin`; the link opens `/auth/email/verify`, which signs
in on a button, never on load, because mail scanners open links.
- `landingPath` keeps every landing on this site. The header, comment and
claim buttons now open the sign-in page rather than Google directly.
- DEVELOPMENT.md and CLAUDE.md describe the flow.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Readers without Google can now sign in by email. One message carries both a Sign in link and a six-digit code, and either one signs the reader in. A first sign-in creates the account and sends the usual welcome email. Google sign-in is unchanged.
Why not allauth's login-by-code
allauth binds its code flow to the
X-Session-Tokenof the browser that asked, so a link opened on a phone could not finish a sign-in started on a laptop. It also has no link variant.apps/accounts/email_login.pyis a small service of our own that ends in the same JWT pair as the Google exchange, so refresh and rotation are unchanged.Backend
POST /api/v1/auth/email-login{email, next}POST /api/v1/auth/email-login/confirm{email, code}or{token}returns the token pair plususer.EMAIL_LOGIN_REQUEST_RATE(10/hour) andEMAIL_LOGIN_CONFIRM_RATE(30/hour), documented in.env.example.EmailAddress, so a later Google sign-in with the same address lands on the same account.login_link.claim_invitenow says "Sign in" rather than "Sign in with Google".Frontend
/auth/signin: "Continue with Google" stays, and an email form sits below it. After sending, the form switches to a code box with a "Use a different email" button.Credentialsprovider with idemail. Itsauthorizemakes the confirm call, andresolveAuthTokenkeeps the pair./auth/email/verify: signs in on a button, never on load, because mail scanners open links. A spent or expired link goes to/auth/error?error=EmailLinkInvalid.landingPathkeeps every landing on this site. next-auth'ssignIn()sends an absolutecallbackUrl, and//hostor/\hostfall back to/account.Testing
make backend-testpasses (795), andtests/test_email_login.pyhas 22 cases.make frontend-testpasses (951).make lintis clean, andmake schemaleaves the tree clean.next, and/accountshowed the new account./auth/signinshowed an inline error for a wrong code, and the right code signed in and redirected.Deploy note
Migration
accounts.0003_email_loginadds theaccounts_email_logintable.