Skip to content

fix(server): say why a change was refused after signing out - #683

Merged
LeMyst merged 1 commit into
cooklang:mainfrom
LeMyst:fix/signed-out-writes
Oct 11, 2026
Merged

LeMyst merged 1 commit into
cooklang:mainfrom
LeMyst:fix/signed-out-writes

Conversation

@LeMyst

@LeMyst LeMyst commented Oct 11, 2026

Copy link
Copy Markdown
Collaborator

Closes #675.

Since sign-in landed (0e8dacf, #534), a page left open after its visitor's sign-in ended still shows the controls of a signed-in user, and the server refuses what they do with them. The page didn't say why: a shopping-list tick quietly unticked itself, and other writes showed the middleware's English Sign in to make changes (or Your role does not allow this change), with no way to sign in again.

What changes

  • The refusals say what to do. static/js/sign-in-notice.js wraps fetch, as unsaved.js already does. When a write (anything other than GET/HEAD) comes back refused by the sign-in middleware, it shows one notice at the top of the page:

    • 401: You're no longer signed in, so this change wasn't made. plus Sign in, linking to /login?next=<this page> through the same data-login-link code as the nav link.
    • 403: the translated role-forbidden text, with no sign-in link, since signing in again wouldn't help.

    This covers every write on every page (shopping list, recipe, menu, planner, pantry, aisles, editor, preferences) without changing their own error handling. The tick is still taken back, because nothing was written, but now the page says why.

  • The middleware marks its own refusals. API 401/403 from auth::middleware carry X-Cook-Refused: sign-in|role. A handler's own 403/404 (such as the CORS origin check) gets no mark, so it never offers to sign in.

  • The API's refusal text is translated by the lang cookie or Accept-Language, like the pages. In English it stays exactly as before (Sign in to make changes, Your role does not allow this change), so existing clients see no difference. The API docs page says so (docs/api.md regenerated).

  • The notice and script are only rendered when a users file is in use (viewer.auth_enabled()) and never in cook build web output, so the no-users server is unchanged.

  • New messages sign-in-to-change, role-forbidden-change and signed-out-notice in all 9 locales.

Each page still shows its own error banner or toast next to the notice. They complement each other: the banner shows the API text (Sign in to make changes), the notice explains it and links to sign-in.

In the editor, following Sign in with unsaved text gets the usual leave-page question from unsaved.js, so edits aren't dropped silently.

Tests

  • tests/server_auth_test.rs: the existing guest/role checks also assert the header; a new an_api_refusal_is_translated_and_marked covers the French texts, the header, no header on a handler's 404, and the translated notice on a signed-in user's page. without_users_the_server_stays_open checks the notice is absent.
  • tests/e2e/sign-in-notice.spec.ts starts its own cook server with a users file (isolated COOK_CONFIG_DIR, temp recipe dir): sign out from another context, tick → notice with Sign in → /login?next=%2Fshopping-list, tick taken back, notice closes; role lowered from shopper to reader in users.toml → role notice without a link.

Checked locally:

  • cargo fmt --check, cargo clippy --all-targets (no warnings) and cargo test pass.
  • The whole Playwright chromium project: 302 passed, 5 skipped, 1 failed. The failure is accessibility.spec.ts on Preferences (link-in-text-block in a card this PR doesn't touch). The shared e2e server has no users file, so this PR renders no new markup there. Main's CI is green, so the failure looks specific to this machine.

each_role_can_do_only_what_it_allows failed once locally (eddie's save read back an empty file), then passed in five reruns. It's an existing race between the save and the read-back, not touched here.

🤖 Generated with Claude Code

Since sign-in landed (0e8dacf, cooklang#534), a page left open after its
visitor's sign-in ended keeps the controls of a signed-in user. What they
do with them was refused without a word: a shopping list tick unticked
itself, and other writes showed the middleware's English error with no
way to sign in again.

The middleware now marks its API 401 and 403 with
`X-Cook-Refused: sign-in|role` and writes their `error` in the visitor's
language (unchanged in English). static/js/sign-in-notice.js watches
every write a page sends and, on a marked refusal, shows one notice: for
a 401, that the change was not made, with a Sign in link back to the
page; for a 403, the translated role-forbidden text. Pages only get the
notice when a users file is in use.

Closes cooklang#675

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@LeMyst
LeMyst marked this pull request as ready for review October 11, 2026 16:40
@LeMyst
LeMyst merged commit c18ae1a into cooklang:main Oct 11, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Signed-out pages don't say a change needs sign-in (ticks undo silently)

1 participant