Client - add URL interceptors to simplify routes definition
This commit is contained in:
26
fittrackee_client/src/utils/history.js
Normal file
26
fittrackee_client/src/utils/history.js
Normal file
@ -0,0 +1,26 @@
|
||||
const pathInterceptor = toPath => {
|
||||
if (
|
||||
!window.localStorage.authToken &&
|
||||
!['/login', '/register'].includes(toPath.pathname)
|
||||
) {
|
||||
toPath.pathname = '/login'
|
||||
}
|
||||
if (
|
||||
window.localStorage.authToken &&
|
||||
['/login', '/register'].includes(toPath.pathname)
|
||||
) {
|
||||
toPath.pathname = '/'
|
||||
}
|
||||
return toPath
|
||||
}
|
||||
|
||||
export const historyEnhancer = originalHistory => {
|
||||
originalHistory.location = pathInterceptor(originalHistory.location)
|
||||
return {
|
||||
...originalHistory,
|
||||
push: (path, ...args) =>
|
||||
originalHistory.push(pathInterceptor(path), ...args),
|
||||
replace: (path, ...args) =>
|
||||
originalHistory.replace(pathInterceptor(path), ...args),
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user