Instruction file imported from dtebar-10010/tq_v02 (
.github/instructions/auth-allauth.instructions.md). Copyright stays with the author.
Auth & django-allauth Instructions
URL ordering rule (CRITICAL)
In tqv02_settings/urls.py, all custom auth views (login, register, password-reset, ajax variants) MUST appear BEFORE path('accounts/', include('allauth.urls')). If allauth's include comes first it shadows /accounts/login/ and the AJAX modal returns HTML instead of JSON.
Symptom of regression: "Invalid email or password" with correct credentials, no JSON in network tab.
Multi-backend login rule
AUTHENTICATION_BACKENDS contains both Django's ModelBackend and allauth's AuthenticationBackend. Any explicit login(request, user) call MUST pass:
login(request, user, backend="django.contrib.auth.backends.ModelBackend")
Omitting backend= raises ValueError: multiple authentication backends configured.
Cart-merge handoff (do NOT bypass)
Both email/password and social logins funnel post-login redirect through TQAccountAdapter.get_login_redirect_url(). If session['pending_merge'] is set, the adapter routes to cart_merge_prompt. New auth flows MUST preserve this; do not call login() and then immediately redirect('home') -- always go through the adapter.
Social-login regressions to honour
| Regression | Rule |
|---|---|
| REG-SOCIAL-001 | templates/socialaccount/*.html MUST extend the site chrome (_base/base.html), not allauth's bare layout |
| REG-SOCIAL-002 | Per-provider SVG logos in the login/connect buttons, not generic icons |
| REG-SOCIAL-003 | "Sign in with X" wording for login, "Connect X" for connecting to existing account -- never share the same string |
| REG-SOCIAL-004 | Error page returns HTTP 401 (not 200) so monitoring catches failures |
| REG-SOCIAL-005 | Signup redirect without session falls back to home, never crashes |
Tests live in tqv02_app/tests/test_views_social_auth.py. Run them after any change to templates/socialaccount/ or allauth_adapter.py.
Provider configuration
- Social provider URLs (
/accounts/<provider>/login/) are registered byinclude('allauth.urls')regardless of DB state. SocialApp.DoesNotExistat request time = admin has not configured the provider in/admin/socialaccount/socialapp/. NOT a URL bug.- Production requires
ACCOUNT_DEFAULT_HTTP_PROTOCOL = 'https'(already set). Without it the callback URL sent to Google ishttp://and rejected. Site.objects.get_current().domainmust match the user-visible domain (www vs non-www) OR both redirect URIs must be registered in the provider console.setup_social_providersmanagement command is the canonical way to upsert providers from.env. Never editSocialApprows directly in fixtures (secret leak class).
Registration flow
register_ajax_viewcreates the user, then callslogin(request, user, backend='...'), then merges session cart viamerge_session_to_db(request, user), then returns JSON.- If a guest had items in the session cart, the response includes
pending_merge=Trueso the modal redirects tocart_merge_prompt. - Email verification: token is generated, persisted on
EmailSubscription(newsletter) orCustomUser.email_verification_token(account). Verify-link views are NOT@login_required.
Password reset
- Uses Django built-in views wired to custom templates in
templates/registration/. - Reset emails go through the standard
PasswordResetForm. Subject and body templates MUST be wrapped in{% trans %}.
Forbidden patterns
login(request, user)withoutbackend=-- ValueError.path('accounts/', include('allauth.urls'))placed before custom auth paths -- shadows/accounts/login/.- Hardcoding Google / Apple / Facebook client secrets in
setup_social_providersdefaults -- read fromos.environ. - Dumping
socialaccount.socialappinto a tracked fixture -- secret leak.
Tests to run after touching this surface
python manage.py test tqv02_app.tests.test_allauth `
tqv02_app.tests.test_allauth_regressions `
tqv02_app.tests.test_guest_auth_matrix `
tqv02_app.tests.test_setup_social_providers `
tqv02_app.tests.test_views_social_auth `
--settings=tqv02_settings.test_settings --keepdb