Last active
February 3, 2020 13:44
-
-
Save Franpastoragusti/d3776d20261362086a524ae96ee7b8d5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* /src/routes/privateRoute.jsx */ | |
import React from "react"; | |
import { Route } from "react-router-dom"; | |
import { AuthConsumer } from "../providers/authProvider"; | |
export const PrivateRoute = ({ component, ...rest }) => { | |
const renderFn = (Component) => (props) => ( | |
<AuthConsumer> | |
{({ isAuthenticated, signinRedirect }) => { | |
if (!!Component && isAuthenticated()) { | |
return <Component {...props} />; | |
} else { | |
signinRedirect(); | |
return <span>loading</span>; | |
} | |
}} | |
</AuthConsumer> | |
); | |
return <Route {...rest} render={renderFn(component)} />; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you! That does help!