diff --git a/fittrackee_client/src/components/App.jsx b/fittrackee_client/src/components/App.jsx index 431a4e99..4b4d6fa7 100644 --- a/fittrackee_client/src/components/App.jsx +++ b/fittrackee_client/src/components/App.jsx @@ -1,5 +1,5 @@ import React from 'react' -import { Redirect, Route, Switch } from 'react-router-dom' +import { Route, Switch } from 'react-router-dom' import './App.css' // import Admin from './Admin' @@ -14,7 +14,6 @@ import Profile from './User/Profile' import ProfileEdit from './User/ProfileEdit' import Statistics from './Statistics' import UserForm from './User/UserForm' -import { isLoggedIn } from '../utils' export default class App extends React.Component { constructor(props) { @@ -27,50 +26,20 @@ export default class App extends React.Component {
- - isLoggedIn() ? : - } - /> + - isLoggedIn() ? ( - - ) : ( - - ) - } + render={() => } /> - isLoggedIn() ? ( - - ) : ( - - ) - } + render={() => } /> - - isLoggedIn() ? : - } - /> - - isLoggedIn() ? : - } - /> + + diff --git a/fittrackee_client/src/index.js b/fittrackee_client/src/index.js index e77285bd..a2e43361 100644 --- a/fittrackee_client/src/index.js +++ b/fittrackee_client/src/index.js @@ -13,8 +13,9 @@ import Root from './components/Root' import registerServiceWorker from './registerServiceWorker' import createRootReducer from './reducers' import { loadProfile } from './actions/user' +import { historyEnhancer } from './utils/history' -export const history = createBrowserHistory() +export const history = historyEnhancer(createBrowserHistory()) history.listen(() => { window.scrollTo(0, 0) diff --git a/fittrackee_client/src/utils/history.js b/fittrackee_client/src/utils/history.js new file mode 100644 index 00000000..3da7b17f --- /dev/null +++ b/fittrackee_client/src/utils/history.js @@ -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), + } +}