Skip to content

Commit 2745a3e

Browse files
author
Brian Genisio
committed
fixing the auto-login/redirect code
1 parent 1bea460 commit 2745a3e

1 file changed

Lines changed: 7 additions & 24 deletions

File tree

client/src/hooks/AuthContext.tsx

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,11 @@ const AuthContextProvider = ({
9191
return;
9292
}
9393

94-
navigate(finalRedirect, { replace: true });
94+
if (finalRedirect.startsWith('http://') || finalRedirect.startsWith('https://')) {
95+
window.location.href = finalRedirect;
96+
} else {
97+
navigate(finalRedirect, { replace: true });
98+
}
9599
}, 50),
96100
[navigate, setUser],
97101
);
@@ -112,15 +116,7 @@ const AuthContextProvider = ({
112116
onError: (error: TResError | unknown) => {
113117
const resError = error as TResError;
114118
doSetError(resError.message);
115-
// Preserve a valid redirect_to across login failures so the deep link survives retries.
116-
// Cannot use buildLoginRedirectUrl() here — it reads the current pathname (already /login)
117-
// and would return plain /login, dropping the redirect_to destination.
118-
const redirectTo = new URLSearchParams(window.location.search).get('redirect_to');
119-
const loginPath =
120-
redirectTo && isSafeRedirect(redirectTo)
121-
? `/login?redirect_to=${encodeURIComponent(redirectTo)}`
122-
: '/login';
123-
navigate(loginPath, { replace: true });
119+
navigateToLogin({ replace: true });
124120
},
125121
});
126122
const logoutUser = useLogoutUserMutation({
@@ -139,7 +135,7 @@ const AuthContextProvider = ({
139135
token: undefined,
140136
isAuthenticated: false,
141137
user: undefined,
142-
redirect: '/login',
138+
redirect: data.redirect ?? '/login',
143139
});
144140
},
145141
onError: (error) => {
@@ -192,19 +188,6 @@ const AuthContextProvider = ({
192188
return;
193189
}
194190
navigateToLogin();
195-
const storedRedirect = sessionStorage.getItem(SESSION_KEY);
196-
sessionStorage.removeItem(SESSION_KEY);
197-
const baseUrl = apiBaseUrl();
198-
const rawPath = window.location.pathname;
199-
const strippedPath =
200-
baseUrl && (rawPath === baseUrl || rawPath.startsWith(baseUrl + '/'))
201-
? rawPath.slice(baseUrl.length) || '/'
202-
: rawPath;
203-
const currentUrl = `${strippedPath}${window.location.search}`;
204-
const fallbackRedirect = isSafeRedirect(currentUrl) ? currentUrl : '/c/new';
205-
const redirect =
206-
storedRedirect && isSafeRedirect(storedRedirect) ? storedRedirect : fallbackRedirect;
207-
setUserContext({ user, token, isAuthenticated: true, redirect });
208191
return;
209192
}
210193
console.log('Token is not present. User is not authenticated.');

0 commit comments

Comments
 (0)