I'm trying to implement protected routes in React. Following is my implementation
if (isAuth()) {
routesToRender = (
<React.Fragment>
{/* <Redirect exact from="/" to="/dashboard" /> */}
<Route path="/" exact component={props => <Dashboard {...props} />} />
<Route path="/dashboard" exact component={props => <Dashboard {...props} />} />
<Route path="/settings/" exact component={props => <Settings {...props} />} />
</React.Fragment>
)
} else {
routesToRender = (
<React.Fragment>
<Route path="/signup/" exact component={props => <Signup {...props} />} />
<Route path="/" exact component={props => <Login {...props} />} />
<Redirect from="*" to="/" />
</React.Fragment>
)
}
If not authenticated I want to redirect all the routes to root URL which is *
and I use <Redirect from="*" to="/" />
for that. But I also want to be able to access /signup
.
How do I redirect from all routes except one ?