jrsowles jrsowles - 1 year ago 72
Javascript Question

React Router requireAuth not redirecting

I have the following routes and requireAuth implementation. I have two problems.

1.) I'm not being redirected from / to /login when I'm not logged in.

2.) The Home component isn't being rendered when I try to access / and I am logged in.

No console errors are showing up in any use case. I've confirmed that IsLoggedIn() is returning the expected value at the expected times. So my requireAuth implementation is likely to blame here. What did I do wrong?


import { isLoggedIn } from './auth';

function requireAuth(nextState, replace, callback) {
if (!isLoggedIn()) {

<Provider store={store}>
<Router history={hashHistory}>
<Route path="/" component={Home} onEnter={requireAuth} />
<Route path="/login" component={Login} />
</Provider>, document.getElementById('root')


module.exports = {
isLoggedIn() {
return localStorage.token != undefined;

Answer Source

If callback is listed as a 3th argument, this hook will run asynchronously, and the transition will block until callback is called. In your case, the callback is not needed and you can safely omit it(as the isLoggedIn is synchronous)