Skip to content

Commit

Permalink
fix: shared link redirecting to login when code block includes "Run C…
Browse files Browse the repository at this point in the history
…ode" button
  • Loading branch information
danny-avila committed Dec 7, 2024
1 parent c5a4732 commit 1efea17
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
7 changes: 6 additions & 1 deletion client/src/components/Messages/Content/RunCode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ const RunCode: React.FC<CodeBarProps> = 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 } =
Expand Down
14 changes: 11 additions & 3 deletions packages/data-provider/src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand All @@ -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';
}
Expand Down

0 comments on commit 1efea17

Please sign in to comment.