Skip to content

Commit 3f4073d

Browse files
committed
Use auth state to track the history
Signed-off-by: Andrea Lamparelli <a.lamparelli95@gmail.com>
1 parent d420bbb commit 3f4073d

File tree

4 files changed

+24
-17
lines changed

4 files changed

+24
-17
lines changed

horreum-web/src/App.tsx

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,13 @@ import About from "./About"
4242
import AppContextProvider from "./context/AppContext";
4343
import TableReportConfigPage from "./domain/reports/TableReportConfigPage";
4444
import TableReportPage from "./domain/reports/TableReportPage";
45-
import {createUserManager, onSigninCallback} from "./auth/oidc";
45+
import {createUserManager} from "./auth/oidc";
4646
import {AuthProvider} from "react-oidc-context";
4747
import {KeycloakConfig} from "./generated";
4848
import {configApi} from "./api";
4949
import AuthBridgeContextProvider, {AuthBridgeContext} from "./context/AuthBridgeContext";
5050
import {AuthContextType} from "./context/@types/authContextTypes";
51+
import {User} from "oidc-client-ts";
5152
import CallbackSSO from "./auth/CallbackSSO";
5253

5354
const router = createBrowserRouter(
@@ -92,20 +93,33 @@ const router = createBrowserRouter(
9293
export default function App() {
9394
const [horreumOidcConfig, setHorreumOidcConfig] = useState<KeycloakConfig | undefined>()
9495

96+
const onSignInCallback = (user: User | undefined) => {
97+
// TODO: find a way to get rid of the CallbackSSO route and use this callback to redirect to the last visited page
98+
const redirectUrl = (user?.state as { history?: string }).history;
99+
window.history.replaceState({}, document.title, redirectUrl);
100+
};
101+
102+
const onSignOutCallback = () => {
103+
window.history.replaceState({}, document.title, window.location.pathname);
104+
};
105+
95106
useEffect(() => {
96107
configApi.keycloak().then(setHorreumOidcConfig)
97108
}, []);
98109

99110
if (!horreumOidcConfig) {
100-
return <Bullseye>
101-
<Spinner/>
102-
</Bullseye>
111+
return (
112+
<Bullseye>
113+
<Spinner/>
114+
</Bullseye>
115+
)
103116
}
104117

105118
// if using oidc let's wrap the entire app with AuthProvider
106119
const userManager = createUserManager(horreumOidcConfig)
120+
107121
return (
108-
<AuthProvider userManager={userManager} onSigninCallback={onSigninCallback}>
122+
<AuthProvider userManager={userManager} onSigninCallback={onSignInCallback} onSignoutCallback={onSignOutCallback}>
109123
{/* if url is empty or null -> use basic authentication */}
110124
<AuthBridgeContextProvider isOidc={horreumOidcConfig.url !== undefined && horreumOidcConfig.url !== ""}>
111125
<AppContextProvider>

horreum-web/src/auth/CallbackSSO.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import {Bullseye, Spinner} from "@patternfly/react-core";
22
import {useAuth} from "react-oidc-context";
33
import {useNavigate} from "react-router-dom";
4-
import {AuthBridgeContext, beforeLoginHistorySession} from "../context/AuthBridgeContext";
54
import {useEffect, useState} from "react";
65

76
// Default callback sso component, which redirects to the last visited page when the login is completed
@@ -14,7 +13,7 @@ function CallbackSSO() {
1413
useEffect(() => {
1514
// redirect only once the authentication stopped loading
1615
if (!isLoading) {
17-
const history = window.sessionStorage.getItem(beforeLoginHistorySession) ?? "/"
16+
const history = (auth.user?.state as { history?: string }).history ?? "/";
1817
navigate(history, {replace: true});
1918
}
2019
}, [isLoading]);

horreum-web/src/auth/oidc.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import {KeycloakConfig} from "../generated";
2-
import {User, UserManager, WebStorageStateStore} from "oidc-client-ts";
1+
import { KeycloakConfig } from "../generated";
2+
import { UserManager, WebStorageStateStore } from "oidc-client-ts";
33

44
export const createUserManager = (config: KeycloakConfig): UserManager => {
55
return new UserManager({
@@ -8,11 +8,7 @@ export const createUserManager = (config: KeycloakConfig): UserManager => {
88
redirect_uri: `${window.location.origin}/callback-sso`,
99
post_logout_redirect_uri: window.location.origin,
1010
userStore: new WebStorageStateStore({ store: window.localStorage }),
11-
monitorSession: true, // this allows cross tab login/logout detection
11+
monitorSession: true,
1212
automaticSilentRenew: true,
1313
})
1414
}
15-
16-
export const onSigninCallback = (_user: User | undefined) => {
17-
window.history.replaceState({}, document.title, window.location.pathname);
18-
};

horreum-web/src/context/AuthBridgeContext.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ function isTester(owner: string, roles: string[]) {
3030
}
3131

3232
export const AuthBridgeContext = React.createContext<AuthContextType | null>(null);
33-
export const beforeLoginHistorySession = "beforeLoginPath"
3433

3534
const signOutCallback = () => window.location.replace(window.location.origin)
3635

@@ -56,8 +55,7 @@ const AuthBridgeContextProvider: React.FC<ContextProviderProps> = ({ isOidc, chi
5655
const signIn = isOidc ?
5756
() => {
5857
// save the current pathname in the local session to let the callback page redirect back
59-
window.sessionStorage.setItem(beforeLoginHistorySession, window.location.pathname)
60-
return auth.signinRedirect()
58+
return auth.signinRedirect({state: { history: `${location.pathname}${location.search}` }})
6159
} :
6260
(username?: string, password?: string) => {
6361
setIsAuthenticated(true)

0 commit comments

Comments
 (0)