How to get the access token and id token using react-auth0-spa?

Hi

I am trying to integrate React with Auth0 and I am using the functions provided by react-auth0-spa.js.
In this, there is a function loginWithRedirect that redirects a user to Auth0 universal login page if the user hasn’t authenticated before.

Once there is a callback from Auth0, it is handled by another function:

 const handleRedirectCallback = async () => {
    setLoading(true);
    await auth0Client.handleRedirectCallback();
    const user = await auth0Client.getUser();
    setLoading(false);
    setIsAuthenticated(true);
    setUser(user);
  };

At this point, I want the user profile as well as the access token and id token. How can I do that?
I could see browser making an API call for the token.

Hi @mohd.ilyas,

Have you looked through our react quickstart? It demonstrates how to get userinfo and an access token. To get the id token you can use the function described here.

If you have trouble let me know and we can walk through it.

Thanks,
Dan

Hi @dan.woda thanks for your response.

I am already aware of getting an access token and id token. Whenever a user logs in with Auth0, there are HTTP calls for /authorize followed by call for oauth/token and in the response of oauth/token, we get access token, id token. I wanted to know if that response is available to us via react-auth0-spa?

thanks

I’m not sure I understand your question. Did you look at the quickstart?

Hi @dan.woda

I was able to get the id and access token.I used getTokenSilently() to get access token and getIDTokenClaims() to get id token.

Thanks for your help.

1 Like

Great! Let me know if you have any more questions.

Dan

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.