I have a react app having a login page. I have total 7 user roles. on entering login credetials and hitting login button, I am able to fetch user role from backend(nodejs) and send it as a response to login page in frontend. I want to redirect the user to their corresponding landing page route after login based on their roles.
How can I achieve this?
I tried this
.then(() => {
if (currentUser.roles[0] === "ROLE_STUDENT") {
props.history.push("/student");
window.location.reload();
} else if (currentUser.roles[0] === "ROLE_TEACHER") {
props.history.push("/teacher");
window.location.reload();
} else if (currentUser.roles[0] === "ROLE_CLASS_TEACHER") {
props.history.push("/class_teacher");
window.location.reload();
} else if (currentUser.roles[0] === "ROLE_PRINCIPAL") {
props.history.push("/principal");
window.location.reload();
} else if (currentUser.roles[0] === "ROLE_ACCOUNT") {
props.history.push("/account");
window.location.reload();
} else if (currentUser.roles[0] === "ROLE_ADMIN") {
props.history.push("/admin");
window.location.reload();
} else if (currentUser.roles[0] === "ROLE_MASTER") {
props.history.push("/master");
window.location.reload();
} else {
props.history.push("/");
window.location.reload();
}
})
But getting empty screen even after routing screenshot after login
Here are routes defined
<MenuState>
<Router>
<Switch>
<Route exact path="/" component={Login} />
<Route exact path="/register" component={Register} />
<Route exact path="/forgot" component={ForgotPassword} />
<Route exact path="/student" component={Student} />
<Route exact path="/teacher" component={TeacherLayout} />
<Route exact path="/principal" component={PrincipalLayout}></Route>
<Route exact path="/accounts" component={AccountsLayout}></Route>
<Redirect to="/" />
</Switch>
</Router>
</MenuState>