Repository navigation
fix(server): say why a change was refused after signing out - #683
Merged
Merged
Conversation
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]>
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.
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(orYour 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.jswrapsfetch, asunsaved.jsalready 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 samedata-login-linkcode as the nav link.403: the translatedrole-forbiddentext, 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/403fromauth::middlewarecarryX-Cook-Refused: sign-in|role. A handler's own403/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
langcookie orAccept-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.mdregenerated).The notice and script are only rendered when a users file is in use (
viewer.auth_enabled()) and never incook build weboutput, so the no-users server is unchanged.New messages
sign-in-to-change,role-forbidden-changeandsigned-out-noticein 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 newan_api_refusal_is_translated_and_markedcovers 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_openchecks the notice is absent.tests/e2e/sign-in-notice.spec.tsstarts its owncook serverwith a users file (isolatedCOOK_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 inusers.toml→ role notice without a link.Checked locally:
cargo fmt --check,cargo clippy --all-targets(no warnings) andcargo testpass.accessibility.spec.tson Preferences (link-in-text-blockin 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_allowsfailed 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