AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
fix(frontend): logout console issues (#11400)
Ubbe3b34c04a7a2c
diff --git a/autogpt_platform/frontend/src/components/layout/Navbar/components/AccountMenu/components/AccountLogoutOption.tsx b/autogpt_platform/frontend/src/components/layout/Navbar/components/AccountMenu/components/AccountLogoutOption.tsxindex 2e16a542db..c462e1c6ac 100644--- a/autogpt_platform/frontend/src/components/layout/Navbar/components/AccountMenu/components/AccountLogoutOption.tsx+++ b/autogpt_platform/frontend/src/components/layout/Navbar/components/AccountMenu/components/AccountLogoutOption.tsx@@ -1,15 +1,13 @@"use client";import { IconLogOut } from "@/components/__legacy__/ui/icons";import { LoadingSpinner } from "@/components/__legacy__/ui/loading";+import { toast } from "@/components/molecules/Toast/use-toast";import { useSupabase } from "@/lib/supabase/hooks/useSupabase";import { cn } from "@/lib/utils";import * as Sentry from "@sentry/nextjs";-import { useRouter } from "next/navigation";import { useTransition } from "react";-import { toast } from "@/components/molecules/Toast/use-toast";export function AccountLogoutOption() {- const router = useRouter();const [isPending, startTransition] = useTransition();const supabase = useSupabase();@@ -17,7 +15,6 @@ export function AccountLogoutOption() {startTransition(async () => {try {await supabase.logOut();- router.refresh();} catch (e) {Sentry.captureException(e);toast({diff --git a/autogpt_platform/frontend/src/components/layout/Navbar/components/NavbarView.tsx b/autogpt_platform/frontend/src/components/layout/Navbar/components/NavbarView.tsxindex a0998a6016..ffe7d810b7 100644--- a/autogpt_platform/frontend/src/components/layout/Navbar/components/NavbarView.tsx+++ b/autogpt_platform/frontend/src/components/layout/Navbar/components/NavbarView.tsx@@ -1,10 +1,11 @@"use client";-import { useMemo } from "react";import { useGetV2GetUserProfile } from "@/app/api/__generated__/endpoints/store/store";import { IconAutoGPTLogo, IconType } from "@/components/__legacy__/ui/icons";import { useBreakpoint } from "@/lib/hooks/useBreakpoint";import { useSupabase } from "@/lib/supabase/hooks/useSupabase";+import { Flag, useGetFlag } from "@/services/feature-flags/use-get-flag";+import { useMemo } from "react";import { getAccountMenuItems, loggedInLinks, loggedOutLinks } from "../helpers";import { AccountMenu } from "./AccountMenu/AccountMenu";import { AgentActivityDropdown } from "./AgentActivityDropdown/AgentActivityDropdown";@@ -12,7 +13,6 @@ import { LoginButton } from "./LoginButton";import { MobileNavBar } from "./MobileNavbar/MobileNavBar";import { NavbarLink } from "./NavbarLink";import { Wallet } from "./Wallet/Wallet";-import { useGetFlag, Flag } from "@/services/feature-flags/use-get-flag";interface NavbarViewProps {isLoggedIn: boolean;}@@ -41,7 +41,7 @@ export const NavbarView = ({ isLoggedIn }: NavbarViewProps) => {<nav className="sticky top-0 z-40 inline-flex h-[60px] w-full items-center border border-white/50 bg-[#f3f4f6]/20 p-3 backdrop-blur-[26px]">{/* Left section */}{!isSmallScreen ? (- <div className="flex flex-1 items-center gap-3 gap-5">+ <div className="flex flex-1 items-center gap-5">{isLoggedIn? linksWithChat.map((link) => (<NavbarLinkdiff --git a/autogpt_platform/frontend/src/lib/autogpt-server-api/client.ts b/autogpt_platform/frontend/src/lib/autogpt-server-api/client.tsindex 0955c0a6be..f410c8d657 100644--- a/autogpt_platform/frontend/src/lib/autogpt-server-api/client.ts+++ b/autogpt_platform/frontend/src/lib/autogpt-server-api/client.ts@@ -1,11 +1,12 @@+import { IMPERSONATION_HEADER_NAME } from "@/lib/constants";+import { ImpersonationState } from "@/lib/impersonation";import { getWebSocketToken } from "@/lib/supabase/actions";import { getServerSupabase } from "@/lib/supabase/server/getServerSupabase";-import { createBrowserClient } from "@supabase/ssr";-import type { SupabaseClient } from "@supabase/supabase-js";+import { environment } from "@/services/environment";import { Key, storage } from "@/services/storage/local-storage";-import { IMPERSONATION_HEADER_NAME } from "@/lib/constants";-import { ImpersonationState } from "@/lib/impersonation";import * as Sentry from "@sentry/nextjs";+import { createBrowserClient } from "@supabase/ssr";+import type { SupabaseClient } from "@supabase/supabase-js";import type {AddUserCreditsResponse,AnalyticsDetails,@@ -70,7 +71,6 @@ import type {UsersBalanceHistoryResponse,WebSocketNotification,} from "./types";-import { environment } from "@/services/environment";const isClient = environment.isClientSide();@@ -1006,8 +1006,13 @@ export default class BackendAPI {// Dynamic import is required even for client-only functions because helpers.ts// has server-only imports (like getServerSupabase) at the top level. Static imports// would bundle server-only code into the client bundle, causing runtime errors.- const { buildClientUrl, buildUrlWithQuery, handleFetchError } =- await import("./helpers");+ const {+ buildClientUrl,+ buildUrlWithQuery,+ handleFetchError,+ isLogoutInProgress,+ isAuthenticationError,+ } = await import("./helpers");const payloadAsQuery = ["GET", "DELETE"].includes(method);let url = buildClientUrl(path);@@ -1034,7 +1039,18 @@ export default class BackendAPI {});if (!response.ok) {- throw await handleFetchError(response);+ const error = await handleFetchError(response);+ if (+ isAuthenticationError(response, error.message) &&+ isLogoutInProgress()+ ) {+ console.debug(+ "Authentication request failed during logout, ignoring:",+ error.message,+ );+ return null;+ }+ throw error;}return await response.json();diff --git a/autogpt_platform/frontend/src/lib/autogpt-server-api/helpers.ts b/autogpt_platform/frontend/src/lib/autogpt-server-api/helpers.tsindex f405c864cc..8607b45da0 100644--- a/autogpt_platform/frontend/src/lib/autogpt-server-api/helpers.ts+++ b/autogpt_platform/frontend/src/lib/autogpt-server-api/helpers.ts@@ -1,7 +1,7 @@+import { IMPERSONATION_HEADER_NAME } from "@/lib/constants";import { getServerSupabase } from "@/lib/supabase/server/getServerSupabase";-import { Key, storage } from "@/services/storage/local-storage";import { environment } from "@/services/environment";-import { IMPERSONATION_HEADER_NAME } from "@/lib/constants";+import { Key, storage } from "@/services/storage/local-storage";import { GraphValidationErrorResponse } from "./types";@@ -221,7 +221,7 @@ export async function parseApiResponse(response: Response): Promise<any> {}}-function isAuthenticationError(+export function isAuthenticationError(response: Response,errorDetail: string,): boolean {@@ -234,7 +234,7 @@ function isAuthenticationError();}-function isLogoutInProgress(): boolean {+export function isLogoutInProgress(): boolean {if (environment.isServerSide()) return false;try {diff --git a/autogpt_platform/frontend/src/lib/supabase/actions.ts b/autogpt_platform/frontend/src/lib/supabase/actions.tsindex f5608accbc..1c7f17440f 100644--- a/autogpt_platform/frontend/src/lib/supabase/actions.ts+++ b/autogpt_platform/frontend/src/lib/supabase/actions.ts@@ -2,7 +2,6 @@import * as Sentry from "@sentry/nextjs";import type { User } from "@supabase/supabase-js";import { revalidatePath } from "next/cache";-import { redirect } from "next/navigation";import { getRedirectPath } from "./helpers";import { getServerSupabase } from "./server/getServerSupabase";@@ -157,8 +156,7 @@ export async function serverLogout(options: ServerLogoutOptions = {}) {const supabase = await getServerSupabase();if (!supabase) {- redirect("/login");- return;+ return { success: true };}try {@@ -170,14 +168,18 @@ export async function serverLogout(options: ServerLogoutOptions = {}) {if (error) {console.error("Error logging out:", error);+ return { success: false, error: error.message };}} catch (error) {console.error("Logout error:", error);+ return {+ success: false,+ error: error instanceof Error ? error.message : "Unknown error",+ };}- // Clear all cached data and redirectrevalidatePath("/", "layout");- redirect("/login");+ return { success: true };},);}diff --git a/autogpt_platform/frontend/src/lib/supabase/hooks/useSupabaseStore.ts b/autogpt_platform/frontend/src/lib/supabase/hooks/useSupabaseStore.tsindex a20b2ac4b4..90536285a5 100644--- a/autogpt_platform/frontend/src/lib/supabase/hooks/useSupabaseStore.ts+++ b/autogpt_platform/frontend/src/lib/supabase/hooks/useSupabaseStore.ts@@ -122,20 +122,16 @@ export const useSupabaseStore = create<SupabaseStoreState>((set, get) => {broadcastLogout();- try {- await serverLogout(options);- } catch (error) {- console.error("Error logging out:", error);- } finally {- set({- user: null,- hasLoadedUser: false,- isUserLoading: false,- });+ set({+ user: null,+ hasLoadedUser: false,+ isUserLoading: false,+ });- if (router) {- router.refresh();- }+ const result = await serverLogout(options);++ if (result.success && router) {+ router.push("/login");}}