{"record":{"id":"9086412e5b5f9352","repo":"toeverything/AFFiNE","slug":"oauth-state-expired-908641","errorCode":"oauth_state_expired","errorMessage":"OAuth state expired, please try again.","messagePattern":"OAuth state expired, please try again\\.","errorType":"exception","errorClass":"OauthStateExpired","httpStatus":400,"severity":"error","filePath":"packages/backend/server/src/plugins/oauth/service.ts","lineNumber":107,"sourceCode":"    redirectUri?: string;\n    client: string;\n    clientNonce: string;\n    clientVersion?: string;\n    callbackUrl: string;\n    redirectBaseUrl: string;\n    redirectAllowedOrigins: string[];\n    redirectTrustedDomains: string[];\n  }) {\n    return await this.call<{ url: string }>({\n      action: 'oauth_preflight',\n      ...input,\n    });\n  }\n\n  async callback(input: {\n    code: string;\n    state: string;\n    clientNonce?: string;\n    issue: SessionIssueInput;\n  }) {\n    return await this.call<NativeOAuthCallback>({\n      action: 'oauth_callback',\n      ...input,\n    });\n  }\n\n  private async call<T>(input: Record<string, unknown>) {\n    try {\n      return await this.runtime.executeAuthSessionCommandV1<T>(input);\n    } catch (error) {\n      const message = String(error);\n      if (message.includes('unknown_oauth_provider')) {\n        throw new UnknownOauthProvider({ name: String(input.provider ?? '') });\n      }\n      if (message.includes('invalid_oauth_callback_state')) {\n        throw new InvalidOauthCallbackState();","sourceCodeStart":89,"sourceCodeEnd":125,"githubUrl":"https://github.com/toeverything/AFFiNE/blob/d897bb3d84099e54a6b3c0bd5f4265f8aa87d190/packages/backend/server/src/plugins/oauth/service.ts#L89-L125","documentation":"OauthStateExpired thrown at packages/backend/server/src/plugins/oauth/service.ts:100 when the 36-char state UUID is well-formed but not found in the challenge store (AuthChallengeStore over SessionCache with a 3-hour TTL, see OAUTH_STATE_TTL_MS). The state was never issued, has already expired, was consumed, or lives on another server node's cache.","triggerScenarios":"Completing the OAuth callback more than 3 hours after preflight; server restart that wiped an in-memory/Redis cache without persistence; multi-instance deployment where the callback hits a node that does not share the session cache; the user's browser replaying an old callback; a state UUID that was fabricated (valid format, never saved).","commonSituations":"Self-hoster running several backend replicas behind a load balancer with cache pointing at different Redis DBs or memory cache; Redis flushed/evicted; user left the login tab open overnight; Docker restart with AFFINE default in-memory cache in production.","solutions":["Have the user restart the sign-in flow — a fresh preflight issues a new state.","For multi-instance deployments, point all nodes at the same shared Redis session cache so states are visible cluster-wide.","If states expire too fast in your environment, ensure the cache backend is persistent (Redis AOF/RDB) rather than in-memory.","Check for clock/TTL misconfiguration or Redis maxmemory eviction policy that is evicting oauth_state keys."],"exampleFix":null,"handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  await oauth.verifyCallback(input);\n} catch (err) {\n  if (err instanceof OauthStateExpired) {\n    // user-facing: 'session expired, please sign in again' -> re-run preflight\n  }\n}","preventionTips":["Share one Redis session cache across all backend replicas.","Use a persistent Redis (AOF/RDB) so restarts do not wipe oauth_state keys.","Complete login promptly; states live 3 hours.","Avoid Redis eviction policies that evict auth_challenge:* keys under memory pressure."],"tags":["oauth","state-expired","cache","distributed-systems","authentication"],"backgroundTag":"oauth-state-expired","analyzedSha":"d897bb3d84099e54a6b3c0bd5f4265f8aa87d190","analyzedAt":"2026-08-18T21:16:52.546Z","contentChangedAt":"2026-08-18T21:16:52.546Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}