fix: redirect to login on session expiry instead of generic error#4593
Open
ravindu0823 wants to merge 2 commits into
Open
fix: redirect to login on session expiry instead of generic error#4593ravindu0823 wants to merge 2 commits into
ravindu0823 wants to merge 2 commits into
Conversation
Expired sessions surfaced as generic per-component toasts because the frontend tRPC client had no global handler for UNAUTHORIZED responses. Add a pure, testable auth-error helper and wire a global QueryCache / MutationCache onError handler into the tRPC client so any UNAUTHORIZED response on a protected page redirects to login, regardless of per-component .catch blocks. Fixes Dokploy#4310 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Address review (blocker): the API reuses the UNAUTHORIZED code for two different situations — a missing/expired session AND an authenticated user who lacks a role or resource permission (146 such throws across the routers). Redirecting on every UNAUTHORIZED would yank a logged-in user off their page whenever they hit any permission-denied path. Tag only the genuine no-session throws (protectedProcedure, and the no-session branch of the cli/admin/enterprise procedures) with a SESSION_EXPIRED sentinel message, and gate the client redirect on that sentinel instead of the bare UNAUTHORIZED code. Permission/role denials now fall through to their normal per-component toast with no navigation. Also extend the public-path allowlist (/accept-invitation, /reset-password, /send-reset-password) so an auth error on those pages never redirects. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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.
What
When a user's session expires while they stay on a page (e.g. saving environment variables on a service), tRPC mutations/queries fail with a generic per-component "error" toast. There is no indication the session expired and no recovery path until the user manually refreshes, which then redirects to login.
Root cause
The frontend tRPC client (
apps/dokploy/utils/api.ts) has no global handler forUNAUTHORIZEDresponses. The backend correctly throwsUNAUTHORIZEDtRPC errors on expired sessions, but those surface only through per-component.catchblocks as generic toasts.Fix
apps/dokploy/utils/auth-error.tswith pure, testable predicates:isUnauthorizedError(error)— true when the error is aTRPCClientErrorwhosedata.code === "UNAUTHORIZED".shouldRedirectOnAuthError(pathname, error)— false for public paths (/,/register*,/invitation*) or non-unauthorized errors; true otherwise.handleAuthError(error)— thin side-effecting wrapper that redirects to/exactly once (module-level once-flag) when on a protected page.apps/dokploy/utils/api.tsnow passes aqueryClientConfigwith a globalQueryCacheandMutationCache(@tanstack/react-query) whoseonErrorcallshandleAuthError, so both queries and mutations trigger the redirect regardless of per-component.catchblocks.Test
apps/dokploy/__test__/utils/auth-error.test.tscoversisUnauthorizedError(UNAUTHORIZED vs other codes / plain Error / null / undefined) andshouldRedirectOnAuthError(public paths return false even with an auth error; protected path + auth error returns true; protected path + non-auth error returns false). Confirmed RED first (module absent), then GREEN:Typecheck:
pnpm --filter=dokploy run typecheckpasses clean.The
handleAuthErrorwindow redirect is not unit-tested (no jsdom in this suite).Fixes #4310
🤖 Generated with Claude Code