-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DESENG-542: Keycloak auth method and redirection fixed (#2456)
* DESENG-542: Authentication fixes * DESENG-542: URL Redirection fix * Updated Changelog * Fixing review comments * Fixing review comment
- Loading branch information
1 parent
02ca3c7
commit 5825b43
Showing
11 changed files
with
137 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import React, { createContext, useState, useEffect } from 'react'; | ||
import { useAppDispatch } from 'hooks'; | ||
import UserService from '../../services/userService'; | ||
import { _kc } from 'constants/tenantConstants'; | ||
const KeycloakData = _kc; | ||
|
||
export interface AuthKeyCloakContextProps { | ||
isAuthenticated: boolean; | ||
isAuthenticating: boolean; // Add a flag to indicate ongoing authentication process | ||
keycloakInstance: Keycloak.default | null; | ||
} | ||
|
||
export const AuthKeyCloakContext = createContext<AuthKeyCloakContextProps>({ | ||
isAuthenticated: false, | ||
isAuthenticating: true, // Initially, authentication is in progress | ||
keycloakInstance: null, // Initial value | ||
}); | ||
|
||
export const AuthKeyCloakContextProvider = ({ children }: { children: JSX.Element | JSX.Element[] }) => { | ||
const dispatch = useAppDispatch(); | ||
const [isAuthenticated, setIsAuthenticated] = useState(false); | ||
const [isAuthenticating, setIsAuthenticating] = useState(true); // State to manage authentication loading status | ||
|
||
useEffect(() => { | ||
const initAuth = async () => { | ||
try { | ||
const authenticated = await KeycloakData.init({ | ||
onLoad: 'check-sso', | ||
silentCheckSsoRedirectUri: window.location.origin + '/silent-check-sso.html', | ||
pkceMethod: 'S256', | ||
checkLoginIframe: false, | ||
}); | ||
setIsAuthenticated(authenticated); // Update authentication state | ||
UserService.setKeycloakInstance(KeycloakData); | ||
UserService.setAuthData(dispatch); | ||
} catch (error) { | ||
console.error('Authentication initialization failed:', error); | ||
} finally { | ||
setIsAuthenticating(false); // Indicate that authentication process is complete | ||
} | ||
}; | ||
initAuth(); | ||
}, [dispatch]); | ||
|
||
// Render children only after the authentication status is determined | ||
return ( | ||
<AuthKeyCloakContext.Provider | ||
value={{ | ||
isAuthenticated, | ||
isAuthenticating, | ||
keycloakInstance: KeycloakData, | ||
}} | ||
> | ||
{!isAuthenticating && children} | ||
</AuthKeyCloakContext.Provider> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters