Skip to content

🀯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

πŸ› fix(desktop): market OAuth expiry triggers wrong re-login modal (#15290)

LiJian2461709de432
diff --git a/packages/desktop-bridge/src/index.ts b/packages/desktop-bridge/src/index.ts
index 8874f7a472f..855ab06d5aa 100644
--- a/packages/desktop-bridge/src/index.ts
+++ b/packages/desktop-bridge/src/index.ts
@@ -32,3 +32,12 @@ export const AUTH_REQUIRED_HEADER = 'X-Auth-Required';
* Used to identify authentication failures in TRPC responses.
*/
export const TRPC_ERROR_CODE_UNAUTHORIZED = 'UNAUTHORIZED' as const;
+
+/**
+ * Sentinel message placed in TRPCError({ code: 'UNAUTHORIZED' }) when the failure
+ * originates from the Market service's own OAuth token, NOT from the user's LobeHub
+ * session. responseMeta checks this to suppress the X-Auth-Required header so the
+ * desktop "re-login to LobeHub" modal is NOT shown; the Market OAuth flow handles it
+ * instead via the market-unauthorized event.
+ */
+export const MARKET_AUTH_REQUIRED_MESSAGE = 'MARKET_AUTH_REQUIRED' as const;
diff --git a/src/layout/AuthProvider/MarketAuth/MarketAuthProvider.tsx b/src/layout/AuthProvider/MarketAuth/MarketAuthProvider.tsx
index 31a5d7795d9..eac08f1545d 100644
--- a/src/layout/AuthProvider/MarketAuth/MarketAuthProvider.tsx
+++ b/src/layout/AuthProvider/MarketAuth/MarketAuthProvider.tsx
@@ -705,14 +705,12 @@ export const MarketAuthProvider = ({ children, isDesktop }: MarketAuthProviderPr
useEffect(() => {
const unsubscribe = marketAuthEvents.on('market-unauthorized', async (event) => {
console.info('[MarketAuth] Received unauthorized event for path:', event.path);
- // Desktop: do not open community auth / profile modals from background API 401s.
- // Only attempt a silent token refresh; Lobe cloud re-auth is handled separately (AuthRequiredModal).
if (isDesktop) {
const refreshed = await refreshToken();
if (!refreshed) {
- console.info(
- '[MarketAuth] Desktop: market 401 β€” refresh failed, skipping community sign-in UI',
- );
+ // Silent refresh failed β€” the Market OAuth token is genuinely expired.
+ // Show the Market auth modal so the user can re-authorize.
+ await handleUnauthorized();
}
return;
}
diff --git a/src/libs/trpc/utils/responseMeta.test.ts b/src/libs/trpc/utils/responseMeta.test.ts
index 1a9542ecc14..c3c8f6a6174 100644
--- a/src/libs/trpc/utils/responseMeta.test.ts
+++ b/src/libs/trpc/utils/responseMeta.test.ts
@@ -1,4 +1,8 @@
-import { AUTH_REQUIRED_HEADER, TRPC_ERROR_CODE_UNAUTHORIZED } from '@lobechat/desktop-bridge';
+import {
+ AUTH_REQUIRED_HEADER,
+ MARKET_AUTH_REQUIRED_MESSAGE,
+ TRPC_ERROR_CODE_UNAUTHORIZED,
+} from '@lobechat/desktop-bridge';
import { TRPCError } from '@trpc/server';
import { describe, expect, it } from 'vitest';
@@ -66,6 +70,19 @@ describe('createResponseMeta', () => {
expect(result.headers?.get(AUTH_REQUIRED_HEADER)).toBe('true');
});
+ it('should NOT set AUTH_REQUIRED_HEADER for Market OAuth UNAUTHORIZED errors', () => {
+ const error = new TRPCError({
+ code: TRPC_ERROR_CODE_UNAUTHORIZED,
+ message: MARKET_AUTH_REQUIRED_MESSAGE,
+ });
+ const result = createResponseMeta({
+ ctx: undefined,
+ errors: [error],
+ });
+
+ expect(result.headers).toBeUndefined();
+ });
+
it('should handle multiple errors where one is UNAUTHORIZED', () => {
const errors = [
new TRPCError({ code: 'BAD_REQUEST' }),
diff --git a/src/libs/trpc/utils/responseMeta.ts b/src/libs/trpc/utils/responseMeta.ts
index c11900d1f85..63cb70ed04c 100644
--- a/src/libs/trpc/utils/responseMeta.ts
+++ b/src/libs/trpc/utils/responseMeta.ts
@@ -1,4 +1,8 @@
-import { AUTH_REQUIRED_HEADER, TRPC_ERROR_CODE_UNAUTHORIZED } from '@lobechat/desktop-bridge';
+import {
+ AUTH_REQUIRED_HEADER,
+ MARKET_AUTH_REQUIRED_MESSAGE,
+ TRPC_ERROR_CODE_UNAUTHORIZED,
+} from '@lobechat/desktop-bridge';
import { type TRPCError } from '@trpc/server';
interface ResponseMetaParams {
@@ -11,11 +15,11 @@ interface ResponseMetaParams {
*
* This function handles:
* 1. Forwarding custom headers from context (ctx.resHeaders)
- * 2. Adding X-Auth-Required header for UNAUTHORIZED errors
+ * 2. Adding X-Auth-Required header for LobeHub session UNAUTHORIZED errors
*
* The X-Auth-Required header allows the desktop app (BackendProxyProtocolManager)
- * to distinguish between real authentication failures (e.g., token expired)
- * and other 401 errors (e.g., invalid API keys).
+ * to distinguish between real LobeHub session failures (e.g., token expired)
+ * and other 401 errors (e.g., invalid API keys, Market OAuth expiry).
*/
export function createResponseMeta({ ctx, errors }: ResponseMetaParams): {
headers: Headers | undefined;
@@ -26,7 +30,13 @@ export function createResponseMeta({ ctx, errors }: ResponseMetaParams): {
: undefined;
const headers = resHeaders ? new Headers(resHeaders) : new Headers();
- const hasUnauthorizedError = errors.some((error) => error.code === TRPC_ERROR_CODE_UNAUTHORIZED);
+ // Only set X-Auth-Required for LobeHub session failures, not for Market OAuth failures.
+ // Market auth errors use MARKET_AUTH_REQUIRED_MESSAGE and are handled by the market-unauthorized
+ // event flow (MarketAuthProvider) rather than the desktop re-login modal.
+ const hasUnauthorizedError = errors.some(
+ (error) =>
+ error.code === TRPC_ERROR_CODE_UNAUTHORIZED && error.message !== MARKET_AUTH_REQUIRED_MESSAGE,
+ );
if (hasUnauthorizedError) {
headers.set(AUTH_REQUIRED_HEADER, 'true');
}
diff --git a/src/server/routers/tools/market.ts b/src/server/routers/tools/market.ts
index b3b64867493..4d617b494b1 100644
--- a/src/server/routers/tools/market.ts
+++ b/src/server/routers/tools/market.ts
@@ -1,3 +1,4 @@
+import { MARKET_AUTH_REQUIRED_MESSAGE } from '@lobechat/desktop-bridge';
import { type CodeInterpreterToolName } from '@lobehub/market-sdk';
import { TRPCError } from '@trpc/server';
import debug from 'debug';
@@ -229,8 +230,7 @@ const execInSandboxHandler = async ({
) {
throw new TRPCError({
code: 'UNAUTHORIZED',
- message:
- 'Market authorization expired. An authorization dialog has been shown to the user. Please wait for the user to complete authorization and then retry the current task.',
+ message: MARKET_AUTH_REQUIRED_MESSAGE,
});
}
@@ -268,8 +268,7 @@ const execInSandboxHandler = async ({
) {
throw new TRPCError({
code: 'UNAUTHORIZED',
- message:
- 'Market authorization expired. An authorization dialog has been shown to the user. Please wait for the user to complete authorization and then retry the current task.',
+ message: MARKET_AUTH_REQUIRED_MESSAGE,
});
}