-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented Hook and Helper function for redirects
This navigate() and window.location.assign() replacement maintains x-ms-routing-name parameter in the query string, and can be used for other query parameters later if need be.
- Loading branch information
1 parent
fb17436
commit 80f0c05
Showing
8 changed files
with
71 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { useLocation, useNavigate, Location } from 'react-router-dom'; | ||
|
||
function getFinalDestination( | ||
destination: string, | ||
location: globalThis.Location | Location = window.location, | ||
) { | ||
let qParams: string = ''; | ||
const msRoutingName = 'x-ms-routing-name'; | ||
if (location.search.includes(msRoutingName)) { | ||
Check failure on line 9 in user-interface/src/lib/hooks/UseCamsNavigator.ts
|
||
const query: Record<string, string> = location.search | ||
.substring(1) | ||
.split('&') | ||
.reduce<Record<string, string>>( | ||
(acc, item) => { | ||
const [key, val] = item.split('='); | ||
if (key && val) acc[key] = val; | ||
return acc; | ||
}, | ||
{} as Record<string, string>, | ||
); | ||
if (query[msRoutingName]) { | ||
qParams += `?${msRoutingName}=${query[msRoutingName]}`; | ||
} | ||
} | ||
return destination + qParams; | ||
} | ||
|
||
export const redirectTo = ( | ||
destination: string, | ||
location: globalThis.Location | Location = window.location, | ||
) => { | ||
window.location.assign(getFinalDestination(destination, location)); | ||
}; | ||
|
||
export default function useCamsNavigator() { | ||
const location = useLocation(); | ||
const navigate = useNavigate(); | ||
|
||
const navigateTo = (destination: string) => { | ||
navigate(getFinalDestination(destination, location)); | ||
}; | ||
|
||
return { | ||
navigateTo, | ||
redirectTo, | ||
}; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
import { isCamsApi } from '@/configuration/apiConfiguration'; | ||
import { LOGOUT_PATH } from './login-library'; | ||
import { redirectTo } from '@/lib/hooks/UseCamsNavigator'; | ||
|
||
export async function http401Hook(response: Response) { | ||
if (response.status === 401 && isCamsApi(response.url)) { | ||
const { host, protocol } = window.location; | ||
const logoutUri = protocol + '//' + host + LOGOUT_PATH; | ||
window.location.assign(logoutUri); | ||
redirectTo(logoutUri); | ||
} | ||
} |
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