Skip to content

Instantly share code, notes, and snippets.

@Franpastoragusti
Last active February 3, 2020 13:44
Show Gist options
  • Save Franpastoragusti/d3776d20261362086a524ae96ee7b8d5 to your computer and use it in GitHub Desktop.
Save Franpastoragusti/d3776d20261362086a524ae96ee7b8d5 to your computer and use it in GitHub Desktop.
/* /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)} />;
};
@PurpleMonkeyWrench
Copy link

Does this private route pass in the user information returned from the authentication call? If I wanted to display the name of the user (returned from authentication) in my header banner, how would I go about accessing that information?

@Franpastoragusti
Copy link
Author

Franpastoragusti commented Jan 31, 2020 via email

@PurpleMonkeyWrench
Copy link

Thank you! That does help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment