I have these routes that I want wrapped into a checkAuth method to see the session state of the visitor. To keep the code clean I separated the checkAuth method to a separate file and imported it into the file with routes declaration:
import {checkAuth} from 'helpers/core'
export default ( store ) => {
return (
<Router history={browserHistory} onEnter={checkAuth.bind(store)}>
<Route path={AUTH_ROUTE} component={AuthLayout}>
<IndexRoute component={AuthView}/>
</Route>
<Route component={CoreLayout}>
<Route path={DASHBOARD_ROUTE} component={AuthView}/>
</Route>
</Router>
)
}
checkAuth needs the store to read state and also dispatch some actions so I'm unsure how to pass it. I tried with bind as you can see in my code but console.log(this) returns undefined inside the method.
Here's the checkAuth code:
export const checkAuth = ( desiredRoute, redirect ) => {
console.log(this);// returns undefined
const state = this.getState();// Cannot read property 'getState' of undefined
const isAuthenticated = state.auth.loggedIn;
....
};