Repository navigation
Answer deleted or left organizations with distinct JSON errors - #153
Merged
Merged
Conversation
An /<account uuid>/api/... request for a deleted account was redirected to
/ (302), and a signed-in shopkeeper who was no longer a member got Pundit's
generic 401, which the apps treat as a sign-out. Now:
- deleted account: 404 {code, error_code: "account_not_found", error_message}
from AccountMiddleware via Api::V1::Shopkeeper::AccountNotFoundController,
JSON whatever the request headers, in the shopkeeper's saved locale (or
Accept-Language without a valid token)
- not a member: 403 {code, error_code: "not_account_member", error_message}
from Api::V1::Shopkeeper::BaseController
Pundit denials inside an organization the shopkeeper belongs to stay 401,
devise_token_auth's invalid-token 401 is unchanged, and non-API paths with
an unknown account id still redirect to /.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01K5hZEFqXhBsUQwSWsgR6eH
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01K5hZEFqXhBsUQwSWsgR6eH
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.
Problem
/<unknown account uuid>/api/...was answered byAccountMiddlewarewith302 Location: /, so clients saw the Rails welcome page (dev) or an HTML/JSON 404 (prod).401 {"code":401,"error_message":"You are not authorized to perform this action."}(BasePolicy's "must be signed in", becausecurrent_accounts_shopkeeperis nil). Android signs out on it; iOS shows a generic error.Change (shared contract with myturntagapi)
/<uuid>/api/...for a deleted account404 {"code":404,"error_code":"account_not_found","error_message":"This organization no longer exists."}(ja:この組織は削除されました。),application/jsonregardless of Accept / Content-Type403 {"code":403,"error_code":"not_account_member","error_message":"You are no longer a member of this organization."}(ja:この組織のメンバーではなくなりました。)401 {code, error_message}401 {"errors":[...]}302 /AccountMiddlewaredispatches unknown-account API requests toApi::V1::Shopkeeper::AccountNotFoundController#show(no route). It includes devise_token_auth'sSetUserByTokenonly to pick the locale: the shopkeeper's saved locale with a valid token, otherwiseAccept-Language.Api::V1::Shopkeeper::BaseControllergetsbefore_action :require_account_membershipafter authentication and locale selection.en.yml/ja.yml(api.shopkeeper.account_not_found,api.shopkeeper.not_account_member).Clients will switch to the personal organization on these error codes instead of signing out.
Tests
test/integration/account_membership_errors_test.rb(17 tests). Written first and seen red: 13 failed (302 / 401 instead of 404 / 403), and the 4 unchanged-behavior tests passed. Also broke the locale selection deliberately to confirm the saved-locale test fails.test/lib/account_middleware_test.rb: unknown id in an API path returns 404 JSON; outside the API it still redirects.test/integration/tenant_and_admin_routes_test.rb: non-member now 403not_account_member(was 401).bin/rails test602 runs, 1514 assertions, 0 failures;bin/rubocop,bin/brakeman,bin/bundler-auditanderb_lintclean.Accept: application/vnd.api+json.🤖 Generated with Claude Code
https://claude.ai/code/session_01K5hZEFqXhBsUQwSWsgR6eH