Skip to content

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): Propagate API auth errors to original requestor (#10716)

Reinier van der Leerda585a34e14e
diff --git a/autogpt_platform/autogpt_libs/autogpt_libs/auth/middleware.py b/autogpt_platform/autogpt_libs/autogpt_libs/auth/middleware.py
index eb583ac1fc..68151452d2 100644
--- a/autogpt_platform/autogpt_libs/autogpt_libs/auth/middleware.py
+++ b/autogpt_platform/autogpt_libs/autogpt_libs/auth/middleware.py
@@ -10,8 +10,8 @@ from starlette.status import HTTP_401_UNAUTHORIZED
from .config import settings
from .jwt_utils import parse_jwt_token
-security = HTTPBearer()
logger = logging.getLogger(__name__)
+bearer_auth = HTTPBearer(auto_error=False)
async def auth_middleware(request: Request):
@@ -20,11 +20,10 @@ async def auth_middleware(request: Request):
logger.warning("Auth disabled")
return {}
- security = HTTPBearer()
- credentials = await security(request)
+ credentials = await bearer_auth(request)
if not credentials:
- raise HTTPException(status_code=401, detail="Authorization header is missing")
+ raise HTTPException(status_code=401, detail="Not authenticated")
try:
payload = parse_jwt_token(credentials.credentials)
diff --git a/autogpt_platform/frontend/src/app/api/proxy/[...path]/route.ts b/autogpt_platform/frontend/src/app/api/proxy/[...path]/route.ts
index 3d12ced37c..ad39185dbd 100644
--- a/autogpt_platform/frontend/src/app/api/proxy/[...path]/route.ts
+++ b/autogpt_platform/frontend/src/app/api/proxy/[...path]/route.ts
@@ -98,8 +98,23 @@ function createResponse(
}
}
-function createErrorResponse(error: unknown): NextResponse {
- console.error("API proxy error:", error);
+function createErrorResponse(
+ error: unknown,
+ path: string,
+ method: string,
+): NextResponse {
+ if (
+ error &&
+ typeof error === "object" &&
+ "status" in error &&
+ [401, 403].includes(error.status as number)
+ ) {
+ // Log this since it indicates a potential frontend bug
+ console.warn(
+ `Authentication error in API proxy for ${method} ${path}:`,
+ "message" in error ? error.message : error,
+ );
+ }
// If it's our custom ApiError, preserve the original status and response
if (error instanceof ApiError) {
@@ -147,7 +162,6 @@ async function handler(
const contentType = req.headers.get("Content-Type");
let responseBody: any;
- const responseStatus: number = 200;
const responseHeaders: Record<string, string> = {
"Content-Type": "application/json",
};
@@ -166,9 +180,13 @@ async function handler(
return createUnsupportedContentTypeResponse(contentType);
}
- return createResponse(responseBody, responseStatus, responseHeaders);
+ return createResponse(responseBody, 200, responseHeaders);
} catch (error) {
- return createErrorResponse(error);
+ return createErrorResponse(
+ error,
+ path.map((s) => `/${s}`).join(""),
+ method,
+ );
}
}
diff --git a/autogpt_platform/frontend/src/components/contextual/PublishAgentModal/components/AgentSelectStep/useAgentSelectStep.ts b/autogpt_platform/frontend/src/components/contextual/PublishAgentModal/components/AgentSelectStep/useAgentSelectStep.ts
index 4ff32afd81..2edf96eede 100644
--- a/autogpt_platform/frontend/src/components/contextual/PublishAgentModal/components/AgentSelectStep/useAgentSelectStep.ts
+++ b/autogpt_platform/frontend/src/components/contextual/PublishAgentModal/components/AgentSelectStep/useAgentSelectStep.ts
@@ -34,7 +34,7 @@ export function useAgentSelectStep({
const agents: Agent[] =
(myAgents?.status === 200 &&
- myAgents.data?.agents
+ myAgents.data.agents
.map(
(agent): Agent => ({
name: agent.agent_name,
diff --git a/autogpt_platform/frontend/src/lib/autogpt-server-api/helpers.ts b/autogpt_platform/frontend/src/lib/autogpt-server-api/helpers.ts
index 98acdd584e..56a41f135b 100644
--- a/autogpt_platform/frontend/src/lib/autogpt-server-api/helpers.ts
+++ b/autogpt_platform/frontend/src/lib/autogpt-server-api/helpers.ts
@@ -261,13 +261,7 @@ export async function makeAuthenticatedRequest(
"Authentication request failed during logout, ignoring:",
errorDetail,
);
- return null;
}
-
- // For authentication errors outside logout, log but don't throw
- // This prevents crashes when session expires naturally
- console.warn("Authentication failed:", errorDetail);
- return null;
}
// For other errors, throw ApiError with proper status code
diff --git a/autogpt_platform/frontend/src/lib/autogpt-server-api/proxy-action.ts b/autogpt_platform/frontend/src/lib/autogpt-server-api/proxy-action.ts
deleted file mode 100644
index f8a98cba32..0000000000
--- a/autogpt_platform/frontend/src/lib/autogpt-server-api/proxy-action.ts
+++ /dev/null
@@ -1,48 +0,0 @@
-"use server";
-
-import * as Sentry from "@sentry/nextjs";
-import {
- buildRequestUrl,
- makeAuthenticatedFileUpload,
- makeAuthenticatedRequest,
-} from "./helpers";
-import { getAgptServerApiUrl } from "@/lib/env-config";
-
-export interface ProxyRequestOptions {
- method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
- path: string;
- payload?: Record<string, any>;
- contentType?: string;
-}
-
-export async function proxyApiRequest({
- method,
- path,
- payload,
- contentType = "application/json",
-}: ProxyRequestOptions) {
- return await Sentry.withServerActionInstrumentation(
- "proxyApiRequest",
- {},
- async () => {
- const baseUrl = getAgptServerApiUrl();
- const url = buildRequestUrl(baseUrl, path, method, payload);
- return makeAuthenticatedRequest(method, url, payload, contentType);
- },
- );
-}
-
-export async function proxyFileUpload(
- path: string,
- formData: FormData,
-): Promise<string> {
- return await Sentry.withServerActionInstrumentation(
- "proxyFileUpload",
- {},
- async () => {
- const baseUrl = getAgptServerApiUrl();
- const url = baseUrl + path;
- return makeAuthenticatedFileUpload(url, formData);
- },
- );
-}