I am doing a very basic authentication workflow by using React Router v4 as follows:
const AuthenticatedRoute = ({ component: Component, ...rest }) => (
<Route {...rest} render={props => (
sessionStorage.getItem('jwt') ? (
<Component {...props} />
) : (
<Redirect to={{
pathname: "/login",
state: { from: props.location }
}} />
)
)}/>
)
ReactDOM.render(
<Provider store={createStoreWithMiddleware(reducers)}>
<BrowserRouter>
<div>
<AuthenticatedRoute exact path="/" component={App} />
<Route path="/login" component={LoginPage} />
</div>
</BrowserRouter>
</Provider>
, document.querySelector('.container'));
The flow works perfectly and when there is no 'jwt' the application is redirected to /login. However, strangely enough it is not getting rendered (i.e. blank page) until I press refresh from the browser.
Not sure how to debug and/or what could possible be the issue as this is a similar approach to most examples found online.