Client - add URL interceptors to simplify routes definition

This commit is contained in:
Sam
2020-01-31 12:46:38 +01:00
parent 34614d5a6c
commit b862a77344
3 changed files with 34 additions and 38 deletions

View 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),
}
}