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)} />; | |
}; |
Hi Kyle, two things, first of all from BE you can write in the token
metadata, if I not remember wrong, so with the token you can extract the
information. I prefer to isolate the user info in other calls, so you
authenticate the user . with credentials, an in this moment with the token
seted and the process of authentication has passed load the user info with
other call, in this way you are maintaining the impersonation of the data.
But if you need for some reason you fetch data from BE in the
AuthServiceCalls (signingRequest, signingSilent...), you can update the
AuthService with a function that return the structure that you need from
the service, and add to the context and provider, in this case the
authConsumer will have access to this new function, and you will be able to
manage the data from the context. However I don't like to add user info in
the token or in the auth service.
I hope it helps.
Bye
El vie., 31 ene. 2020 a las 18:02, Kyle Peters (<notifications@github.com>)
escribió:
… 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?
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<https://gist.github.com/d3776d20261362086a524ae96ee7b8d5?email_source=notifications&email_token=ADUHGPQHY3DFO2JYEN422BDRARKRZA5CNFSM4KOKHBSKYY3PNVWWK3TUL52HS4DFVNDWS43UINXW23LFNZ2KUY3PNVWWK3TUL5UWJTQAGBAWI#gistcomment-3162468>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ADUHGPSNRT5O7AENTYC73GLRARKRZANCNFSM4KOKHBSA>
.
Thank you! That does help!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?