2020-05-17 18:20:15 +02:00
|
|
|
const routesWithoutAuthentication = [
|
|
|
|
'/login',
|
|
|
|
'/register',
|
|
|
|
'/password-reset',
|
|
|
|
'/password-reset/request',
|
|
|
|
'/password-reset/sent',
|
|
|
|
'/updated-password',
|
|
|
|
]
|
|
|
|
|
2020-01-31 15:06:11 +01:00
|
|
|
const updatePath = (toPath, newPath) => {
|
|
|
|
if (typeof toPath === 'string' || toPath instanceof String) {
|
|
|
|
toPath = newPath
|
|
|
|
} else {
|
|
|
|
toPath.pathname = newPath
|
|
|
|
}
|
|
|
|
return toPath
|
|
|
|
}
|
|
|
|
|
2020-01-31 12:46:38 +01:00
|
|
|
const pathInterceptor = toPath => {
|
|
|
|
if (
|
|
|
|
!window.localStorage.authToken &&
|
2020-05-17 18:20:15 +02:00
|
|
|
!routesWithoutAuthentication.includes(toPath.pathname)
|
2020-01-31 12:46:38 +01:00
|
|
|
) {
|
2020-01-31 15:06:11 +01:00
|
|
|
toPath = updatePath(toPath, '/login')
|
2020-01-31 12:46:38 +01:00
|
|
|
}
|
|
|
|
if (
|
|
|
|
window.localStorage.authToken &&
|
2020-05-17 18:20:15 +02:00
|
|
|
routesWithoutAuthentication.includes(toPath.pathname)
|
2020-01-31 12:46:38 +01:00
|
|
|
) {
|
2020-01-31 15:06:11 +01:00
|
|
|
toPath = updatePath(toPath, '/')
|
2020-01-31 12:46:38 +01:00
|
|
|
}
|
|
|
|
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),
|
|
|
|
}
|
|
|
|
}
|