From 358d9cb3f4dfeae3e6522d24a98c0a5dbfe3e8a8 Mon Sep 17 00:00:00 2001 From: "sp.wack" <83104063+amanape@users.noreply.github.com> Date: Fri, 24 Jan 2025 16:49:50 +0200 Subject: [PATCH] hotfix(frontend): Logout and clear token if retrieving user fails (#6436) --- .../components/features/sidebar/sidebar.test.tsx | 4 +++- frontend/src/hooks/query/use-github-user.ts | 8 +++++++- frontend/src/mocks/handlers.ts | 2 +- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/frontend/__tests__/components/features/sidebar/sidebar.test.tsx b/frontend/__tests__/components/features/sidebar/sidebar.test.tsx index 1f2ad6c57a3e..dbba5c079f03 100644 --- a/frontend/__tests__/components/features/sidebar/sidebar.test.tsx +++ b/frontend/__tests__/components/features/sidebar/sidebar.test.tsx @@ -155,7 +155,9 @@ describe("Sidebar", () => { const settingsModal = screen.getByTestId("ai-config-modal"); // Click the advanced options switch to show the API key input - const advancedOptionsSwitch = within(settingsModal).getByTestId("advanced-option-switch"); + const advancedOptionsSwitch = within(settingsModal).getByTestId( + "advanced-option-switch", + ); await user.click(advancedOptionsSwitch); const apiKeyInput = within(settingsModal).getByLabelText(/API\$KEY/i); diff --git a/frontend/src/hooks/query/use-github-user.ts b/frontend/src/hooks/query/use-github-user.ts index 9841f6524cda..7d24e12abce0 100644 --- a/frontend/src/hooks/query/use-github-user.ts +++ b/frontend/src/hooks/query/use-github-user.ts @@ -6,7 +6,7 @@ import { useConfig } from "./use-config"; import OpenHands from "#/api/open-hands"; export const useGitHubUser = () => { - const { gitHubToken, setUserId } = useAuth(); + const { gitHubToken, setUserId, logout } = useAuth(); const { data: config } = useConfig(); const user = useQuery({ @@ -29,5 +29,11 @@ export const useGitHubUser = () => { } }, [user.data]); + React.useEffect(() => { + if (user.isError) { + logout(); + } + }, [user.isError]); + return user; }; diff --git a/frontend/src/mocks/handlers.ts b/frontend/src/mocks/handlers.ts index 9d1a42d4305d..d9c40ff146f7 100644 --- a/frontend/src/mocks/handlers.ts +++ b/frontend/src/mocks/handlers.ts @@ -141,7 +141,7 @@ export const handlers = [ { id: 2, full_name: "octocat/earth" }, ]), ), - http.get("https://api.github.com/user", () => { + http.get("/api/github/user", () => { const user: GitHubUser = { id: 1, login: "octocat",