How to implement authenticated routes in React Router 4?
Tnx Tyler McGinnis for solution. I make my idea from Tyler McGinnis idea.
const DecisionRoute = ({ trueComponent, falseComponent, decisionFunc, ...rest }) => {
return (
<Route
{...rest}
render={
decisionFunc()
? trueComponent
: falseComponent
}
/>
)
}
You can implement that like this
<DecisionRoute path="/signin" exact={true}
trueComponent={redirectStart}
falseComponent={SignInPage}
decisionFunc={isAuth}
/>
decisionFunc just a function that return true or false
const redirectStart = props => <Redirect to="/orders" />
You're going to want to use the Redirect
component. There's a few different approaches to this problem. Here's one I like, have a PrivateRoute component that takes in an authed
prop and then renders based on that props.
function PrivateRoute ({component: Component, authed, ...rest}) {
return (
<Route
{...rest}
render={(props) => authed === true
? <Component {...props} />
: <Redirect to={{pathname: '/login', state: {from: props.location}}} />}
/>
)
}
Now your Route
s can look something like this
<Route path='/' exact component={Home} />
<Route path='/login' component={Login} />
<Route path='/register' component={Register} />
<PrivateRoute authed={this.state.authed} path='/dashboard' component={Dashboard} />
If you're still confused, I wrote this post that may help - Protected routes and authentication with React Router v4
All answers are outdated
In 2021 the render
prop of the Route
component is for legacy use according to the react-router-dom documentation and in my case it wasn't even working (I'm using react-router-dom 5.2.0).
This works instead:
import React, { FC } from "react";
import { Route } from "react-router-dom";
const RouteRequiresLogin: FC<React.ComponentProps<typeof Route>> = props => {
const userIsLogged = useLoginStatus();
return (
<Route {...props}>{userIsLogged ? props.children : <LoginPage/>}</Route>
);
};
export default RouteRequiresLogin;
Usage:
/* A route that requires login */
<RouteRequiresLogin path="/dashboard">
<DashboardPage />
</RouteRequiresLogin>
/* A route that doesn't require login */
<Route path="/sign-up">
<SignUpPage />
</Route>