Skip to content

fix(accounts): always set session cookie on login regardless of OL account lookup#12876

Merged
jimchamp merged 2 commits into
masterfrom
12857/fix-login-session-cookie
Jun 8, 2026
Merged

fix(accounts): always set session cookie on login regardless of OL account lookup#12876
jimchamp merged 2 commits into
masterfrom
12857/fix-login-session-cookie

Conversation

@mekarpeles

Copy link
Copy Markdown
Member

Summary

Fixes a regression introduced by #11052 where users with an IA/OL email mismatch (or no linked OL account) would authenticate successfully but silently receive no session cookie, causing an infinite login loop with no error message.

Root cause: _set_login_cookies was moved inside an if ol_account := OpenLibraryAccount.get_by_email(email): guard in #11052. If that lookup returns None, the session cookie is never set, the browser gets no session, and the next request is treated as unauthenticated.

Fix: Move _set_login_cookies outside the guard, matching the OTP login flow (account_login_otp_redeem) which already calls it unconditionally with a potentially-None ol_account. _set_login_cookies already handles ol_account=None correctly.

Before (broken):

if ol_account := OpenLibraryAccount.get_by_email(email):
    _set_login_cookies(audit, ol_account, remember=remember)   # never called if lookup fails
    if web.cookies().get("pda"):
        ...

After (fixed):

ol_account = OpenLibraryAccount.get_by_email(email)
_set_login_cookies(audit, ol_account, remember=remember)       # always called
if ol_account and web.cookies().get("pda"):
    ...

Test plan

  • Log in with an account where IA and OL emails match — session cookie set, redirect to /account/books
  • Log in with an account where OL account lookup returns None — session cookie still set, no silent loop
  • remember me checkbox — session cookie has year-long expiry
  • PDA cookie flow unaffected (pda banner still shows when ol_account is found)
  • OTP login flow unaffected (separate code path, already correct)

Fixes #12857

…count lookup

`_set_login_cookies` was gated inside `if ol_account := OpenLibraryAccount.get_by_email(email)`
so users whose OL account could not be resolved by email (e.g. IA/OL email mismatch)
would authenticate successfully but never receive a session cookie, causing a silent
redirect back to the login page.

Move `_set_login_cookies` outside the guard, matching the OTP login flow which already
calls it unconditionally. `_set_login_cookies` already handles `ol_account=None`
gracefully (sets session + pd cookies, skips sfw/yrg_banner_pref extras).

Fixes #12857
Copilot AI review requested due to automatic review settings June 8, 2026 17:40
@github-actions github-actions Bot added the Priority: 1 Do this week, receiving emails, time sensitive, . [managed] label Jun 8, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a login regression in the upstream account plugin where users could authenticate via IA but receive no Open Library session cookie when OpenLibraryAccount.get_by_email(...) returns None, resulting in a silent login loop.

Changes:

  • Always call _set_login_cookies(...) after a successful audit, regardless of whether an OL account lookup succeeds.
  • Keep the PDA flash-message behavior gated on ol_account while ensuring the session cookie is still set.
  • Reorders one import near the top of the module (but currently in a way that likely conflicts with Ruff/isort expectations).
Comments suppressed due to low confidence (1)

openlibrary/plugins/upstream/account.py:14

  • Import order likely violates the repo’s Ruff/isort import grouping: third-party imports (requests, web) should come before first-party imports (infogami/openlibrary). Having import infogami.core.code as core above requests/web may trigger Ruff I (isort) and is inconsistent with other upstream modules (e.g. openlibrary/plugins/upstream/code.py keeps import web before from infogami ...).
import requests
import web

import infogami.core.code as core  # noqa: F401 side effects may be needed

Comment thread openlibrary/plugins/upstream/account.py
@jimchamp jimchamp merged commit 8220e9d into master Jun 8, 2026
8 checks passed
@jimchamp jimchamp deleted the 12857/fix-login-session-cookie branch June 8, 2026 20:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Priority: 1 Do this week, receiving emails, time sensitive, . [managed]

Projects

None yet

Development

Successfully merging this pull request may close these issues.

openlibrary.org login fails; archive.org succeeds but doesn't work for openlibrary.org

3 participants