From 1efea1738d4304371174c20d869ea8b2543ba5e8 Mon Sep 17 00:00:00 2001 From: Danny Avila Date: Sat, 7 Dec 2024 16:37:42 -0500 Subject: [PATCH] fix: shared link redirecting to login when code block includes "Run Code" button --- client/src/components/Messages/Content/RunCode.tsx | 7 ++++++- packages/data-provider/src/request.ts | 14 +++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/client/src/components/Messages/Content/RunCode.tsx b/client/src/components/Messages/Content/RunCode.tsx index b2190834682..e80c589bd1a 100644 --- a/client/src/components/Messages/Content/RunCode.tsx +++ b/client/src/components/Messages/Content/RunCode.tsx @@ -21,7 +21,12 @@ const RunCode: React.FC = React.memo(({ lang, codeRef, blockIndex const { messageId, conversationId, partIndex } = useMessageContext(); const normalizedLang = useMemo(() => normalizeLanguage(lang), [lang]); - const { data } = useVerifyAgentToolAuth({ toolId: Tools.execute_code }); + const { data } = useVerifyAgentToolAuth( + { toolId: Tools.execute_code }, + { + retry: 1, + }, + ); const authType = useMemo(() => data?.message ?? false, [data?.message]); const isAuthenticated = useMemo(() => data?.authenticated ?? false, [data?.authenticated]); const { methods, onSubmit, isDialogOpen, setIsDialogOpen, handleRevokeApiKey } = diff --git a/packages/data-provider/src/request.ts b/packages/data-provider/src/request.ts index c19d60f78a5..8e0af5f74f3 100644 --- a/packages/data-provider/src/request.ts +++ b/packages/data-provider/src/request.ts @@ -63,7 +63,8 @@ async function _patch(url: string, data?: any) { let isRefreshing = false; let failedQueue: { resolve: (value?: any) => void; reject: (reason?: any) => void }[] = []; -const refreshToken = (retry?: boolean) => _post(endpoints.refreshToken(retry)); +const refreshToken = (retry?: boolean): Promise<{ token?: string } | undefined> => + _post(endpoints.refreshToken(retry)); const dispatchTokenUpdatedEvent = (token: string) => { setTokenHeader(token); @@ -90,6 +91,7 @@ axios.interceptors.response.use( } if (error.response.status === 401 && !originalRequest._retry) { + console.warn('401 error, refreshing token'); originalRequest._retry = true; if (isRefreshing) { @@ -107,16 +109,22 @@ axios.interceptors.response.use( isRefreshing = true; try { - const { token } = await refreshToken( + const response = await refreshToken( // Handle edge case where we get a blank screen if the initial 401 error is from a refresh token request - originalRequest.url?.includes('api/auth/refresh') ? true : false, + originalRequest.url?.includes('api/auth/refresh') === true ? true : false, ); + const token = response?.token ?? ''; + if (token) { originalRequest.headers['Authorization'] = 'Bearer ' + token; dispatchTokenUpdatedEvent(token); processQueue(null, token); return await axios(originalRequest); + } else if (window.location.href.includes('share/')) { + console.log( + `Refresh token failed from shared link, attempting request to ${originalRequest.url}`, + ); } else { window.location.href = '/login'; }