Fix so redirect URL is in OAuth state (#8653)

This commit is contained in:
chuckbutkus 2025-05-23 13:09:47 -04:00 committed by GitHub
parent 2bb1b7b7aa
commit 92f8061558
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 2 additions and 96 deletions

View File

@ -1,12 +1,7 @@
import { useEffect } from "react";
import { useNavigate } from "react-router";
import { useConfig } from "./query/use-config";
import { useIsAuthed } from "./query/use-is-authed";
import {
getLoginMethod,
getLastPage,
LoginMethod,
} from "#/utils/local-storage";
import { getLoginMethod, LoginMethod } from "#/utils/local-storage";
import { useAuthUrl } from "./use-auth-url";
/**
@ -14,7 +9,6 @@ import { useAuthUrl } from "./use-auth-url";
* Only works in SAAS mode and when the user is not already logged in
*/
export const useAutoLogin = () => {
const navigate = useNavigate();
const { data: config, isLoading: isConfigLoading } = useConfig();
const { data: isAuthed, isLoading: isAuthLoading } = useIsAuthed();
@ -71,30 +65,4 @@ export const useAutoLogin = () => {
githubAuthUrl,
gitlabAuthUrl,
]);
// Handle navigation to last page after login
useEffect(() => {
// Only navigate in SAAS mode
if (config?.APP_MODE !== "saas") {
return;
}
// Wait for auth to load
if (isAuthLoading) {
return;
}
// Only navigate if authenticated
if (!isAuthed) {
return;
}
// Get the last page from local storage
const lastPage = getLastPage();
// Navigate to the last page if it exists
if (lastPage) {
navigate(lastPage);
}
}, [config?.APP_MODE, isAuthed, isAuthLoading, navigate]);
};

View File

@ -1,33 +0,0 @@
import { useEffect } from "react";
import { useLocation } from "react-router";
import { useConfig } from "./query/use-config";
import { setLastPage, shouldExcludePath } from "#/utils/local-storage";
import { useIsAuthed } from "./query/use-is-authed";
/**
* Hook to track the last visited page in local storage
* Only tracks pages in SAAS mode and excludes certain paths
*/
export const useTrackLastPage = () => {
const location = useLocation();
const { data: config } = useConfig();
const { data: isAuthed, isLoading: isAuthLoading } = useIsAuthed();
useEffect(() => {
// Only track pages in SAAS mode when authenticated
if (config?.APP_MODE !== "saas" || !isAuthed || isAuthLoading) {
return;
}
const { pathname } = location;
// Don't track excluded paths
if (shouldExcludePath(pathname)) {
// leave code block for now as we may decide not to track certain pages.
// return;
}
// Store the current path as the last visited page
setLastPage(pathname);
}, [location, config?.APP_MODE]);
};

View File

@ -22,7 +22,6 @@ import { useBalance } from "#/hooks/query/use-balance";
import { SetupPaymentModal } from "#/components/features/payment/setup-payment-modal";
import { displaySuccessToast } from "#/utils/custom-toast-handlers";
import { useIsOnTosPage } from "#/hooks/use-is-on-tos-page";
import { useTrackLastPage } from "#/hooks/use-track-last-page";
import { useAutoLogin } from "#/hooks/use-auto-login";
import { LOCAL_STORAGE_KEYS } from "#/utils/local-storage";
@ -86,9 +85,6 @@ export default function MainApp() {
const [consentFormIsOpen, setConsentFormIsOpen] = React.useState(false);
// Track the last visited page
useTrackLastPage();
// Auto-login if login method is stored in local storage
useAutoLogin();

View File

@ -16,5 +16,5 @@ export const generateAuthUrl = (identityProvider: string, requestUrl: URL) => {
authUrl = `auth.${requestUrl.hostname}`;
}
const scope = "openid email profile"; // OAuth scope - not user-facing
return `https://${authUrl}/realms/allhands/protocol/openid-connect/auth?client_id=allhands&kc_idp_hint=${identityProvider}&response_type=code&redirect_uri=${encodeURIComponent(redirectUri)}&scope=${encodeURIComponent(scope)}`;
return `https://${authUrl}/realms/allhands/protocol/openid-connect/auth?client_id=allhands&kc_idp_hint=${identityProvider}&response_type=code&redirect_uri=${encodeURIComponent(redirectUri)}&scope=${encodeURIComponent(scope)}&state=${encodeURIComponent(requestUrl.href)}`;
};

View File

@ -1,7 +1,6 @@
// Local storage keys
export const LOCAL_STORAGE_KEYS = {
LOGIN_METHOD: "openhands_login_method",
LAST_PAGE: "openhands_last_page",
};
// Login methods
@ -27,33 +26,9 @@ export const getLoginMethod = (): LoginMethod | null => {
return method as LoginMethod | null;
};
/**
* Set the last visited page in local storage
* @param path The path of the last visited page
*/
export const setLastPage = (path: string): void => {
localStorage.setItem(LOCAL_STORAGE_KEYS.LAST_PAGE, path);
};
/**
* Get the last visited page from local storage
* @returns The last visited page or null if not set
*/
export const getLastPage = (): string | null =>
localStorage.getItem(LOCAL_STORAGE_KEYS.LAST_PAGE);
/**
* Clear login method and last page from local storage
*/
export const clearLoginData = (): void => {
localStorage.removeItem(LOCAL_STORAGE_KEYS.LOGIN_METHOD);
localStorage.removeItem(LOCAL_STORAGE_KEYS.LAST_PAGE);
};
/**
* Check if the given path should be excluded from being saved as the last page
* @param path The path to check
* @returns True if the path should be excluded, false otherwise
*/
export const shouldExcludePath = (path: string): boolean =>
path.startsWith("/settings");