my app component redirects a used based if they are logged in = true, I used an online example after trying various methods, if there is a more simple functional react way to do it, please show me,
If user is Logged in , I want them to go to /createpost else I want them to go to the /login route, I want to protect the /createpost route that user has to be logged in, but that is a task for another time, though ideas appreciated..
sandbox: https://codesandbox.io/s/focused-sammet-eitqr
my app component
const App = () => {
return (
<Router>
<Switch>
<Route component={Navbar} />
<Route exact path="/Home" component={Home} />
<Route exact path="/posts" component={ViewPost} />
<Route exact path="/login" component={Login} />
<Route exact path="/register" component={Register} />
<PrivateRoute
path="/createpost"
component={CreatePost}
isAuthenticated={false}
/>
</Switch>
</Router>
);
};
PrivateRoute component
export const PrivateRoute = ({
component: Component,
isAuthenticated,
...rest
}) => (
<Route
{...rest}
render={props =>
isAuthenticated ? (
<Component {...props} />
) : (
<Redirect
to={{
pathname: "/login",
state: { from: props.location }
}}
/>
)
}
/>
);