I am trying to create routes dynamically based on login user type. This is my code:
createRoute(){
var allRoutes = [];
if(this.props.userType === "admin"){
allRoutes.push((<Route path="Login" component={Login} />));
allRoutes.push((<Route path="Dashboard" component={AdminDashboard} />));
allRoutes.push((<Route path="UserManagement" component={UserManagement} />));
allRoutes.push((<Route path="ResourceManagement" component={ResourceManagement} />));
allRoutes.push((<Route from='*' to='Dashboard' />));
}
else if(this.props.userType === "user"){
allRoutes.push((<Route path="Login" component={Login} />));
allRoutes.push((<Route path="Dashboard" component={UserDashboard} />));
allRoutes.push((<Route from='*' to='Dashboard' />));
}
else{
allRoutes.push((<Route path="Login" component={Login} />));
allRoutes.push((<Route from='*' to='Login' />));
}
return allRoutes;
}
and this is how I am rendering those
render(){
return (
<div className="contentArea">
<Router history={hashHistory}>
<Route path="/">
{this.createRoute()}
</Route>
</Router>
</div>
);
}
Now suppose if I login with admin user for the first time it is showing the related page properly.
But when I logout and login with normal user it still shows the admin page, until I refresh the whole page.
In short Routes are not getting immediately when different type of users are login in, until we refresh the page.
Can any one please tell me what I am doing wrong here.
Thanks in advance.