Compare commits

...

2 Commits

Author SHA1 Message Date
openhands
78c5f5578b Fix: Only redirect to last page after actual login, not on refresh or new tab 2025-05-23 03:09:34 +00:00
Chuck Butkus
815cc8e660 Only redirect to last page if going to home page 2025-05-22 22:51:19 -04:00
3 changed files with 49 additions and 4 deletions

View File

@@ -9,7 +9,11 @@ import GitHubLogo from "#/assets/branding/github-logo.svg?react";
import GitLabLogo from "#/assets/branding/gitlab-logo.svg?react";
import { useAuthUrl } from "#/hooks/use-auth-url";
import { GetConfigResponse } from "#/api/open-hands.types";
import { LoginMethod, setLoginMethod } from "#/utils/local-storage";
import {
LoginMethod,
setLoginMethod,
setJustLoggedIn,
} from "#/utils/local-storage";
interface AuthModalProps {
githubAuthUrl: string | null;
@@ -29,6 +33,8 @@ export function AuthModal({ githubAuthUrl, appMode }: AuthModalProps) {
// Store the login method in local storage (only in SAAS mode)
if (appMode === "saas") {
setLoginMethod(LoginMethod.GITHUB);
// Set the "just logged in" flag to true
setJustLoggedIn(true);
}
// Always start the OIDC flow, let the backend handle TOS check
window.location.href = githubAuthUrl;
@@ -40,6 +46,8 @@ export function AuthModal({ githubAuthUrl, appMode }: AuthModalProps) {
// Store the login method in local storage (only in SAAS mode)
if (appMode === "saas") {
setLoginMethod(LoginMethod.GITLAB);
// Set the "just logged in" flag to true
setJustLoggedIn(true);
}
// Always start the OIDC flow, let the backend handle TOS check
window.location.href = gitlabAuthUrl;

View File

@@ -5,6 +5,8 @@ import { useIsAuthed } from "./query/use-is-authed";
import {
getLoginMethod,
getLastPage,
getJustLoggedIn,
setJustLoggedIn,
LoginMethod,
} from "#/utils/local-storage";
import { useAuthUrl } from "./use-auth-url";
@@ -59,6 +61,9 @@ export const useAutoLogin = () => {
// If we have an auth URL, redirect to it
if (authUrl) {
// Set the "just logged in" flag to true
setJustLoggedIn(true);
// After successful login, the user will be redirected back and can navigate to the last page
window.location.href = authUrl;
}
@@ -92,8 +97,21 @@ export const useAutoLogin = () => {
// Get the last page from local storage
const lastPage = getLastPage();
// Navigate to the last page if it exists
if (lastPage) {
// Get the current pathname
const currentPath = window.location.pathname;
// Check if the user just logged in
// Only navigate to the last page if:
// 1. Last page exists in local storage
// 2. We're on the home page (/) - this prevents redirecting when a user
// explicitly navigates to a specific page or opens a link in a new tab
// 3. The user just logged in (new condition)
if (lastPage && currentPath === "/" && getJustLoggedIn()) {
// Clear the "just logged in" flag
setJustLoggedIn(false);
// Navigate to the last page
navigate(lastPage);
}
}, [config?.APP_MODE, isAuthed, isAuthLoading, navigate]);

View File

@@ -2,6 +2,7 @@
export const LOCAL_STORAGE_KEYS = {
LOGIN_METHOD: "openhands_login_method",
LAST_PAGE: "openhands_last_page",
JUST_LOGGED_IN: "openhands_just_logged_in",
};
// Login methods
@@ -43,11 +44,12 @@ export const getLastPage = (): string | null =>
localStorage.getItem(LOCAL_STORAGE_KEYS.LAST_PAGE);
/**
* Clear login method and last page from local storage
* Clear login method, last page, and just logged in flag from local storage
*/
export const clearLoginData = (): void => {
localStorage.removeItem(LOCAL_STORAGE_KEYS.LOGIN_METHOD);
localStorage.removeItem(LOCAL_STORAGE_KEYS.LAST_PAGE);
localStorage.removeItem(LOCAL_STORAGE_KEYS.JUST_LOGGED_IN);
};
/**
@@ -57,3 +59,20 @@ export const clearLoginData = (): void => {
*/
export const shouldExcludePath = (path: string): boolean =>
path.startsWith("/settings");
/**
* Set the "just logged in" flag in local storage
* @param value True if the user just logged in, false otherwise
*/
export const setJustLoggedIn = (value: boolean): void => {
localStorage.setItem(LOCAL_STORAGE_KEYS.JUST_LOGGED_IN, value.toString());
};
/**
* Get the "just logged in" flag from local storage
* @returns True if the user just logged in, false otherwise
*/
export const getJustLoggedIn = (): boolean => {
const value = localStorage.getItem(LOCAL_STORAGE_KEYS.JUST_LOGGED_IN);
return value === "true";
};