Search code examples
react-nativeexpoauth0

Getting an id_token using Auth0 in Expo app


I’m implementing Auth0 authentication in a new Expo/React Native app following this example: https://github.com/expo/auth0-example

The only thing I changed is the scope: 'openid profile' which in the example is scope: 'openid name' though I also tried it with the code in the example.

As you can see in the following screen shot, I’m getting an access_token instead of id_token: enter image description here

Here's the code to authenticate with Auth0:

_loginWithAuth0 = async () => {
    const redirectUrl = AuthSession.getRedirectUrl();
    console.log(`Redirect URL (add this to Auth0): ${redirectUrl}`);
    const result = await AuthSession.startAsync({
      authUrl: `${auth0Domain}/authorize` + toQueryString({
        client_id: auth0ClientId,
        response_type: 'token',
        scope: 'openid profile',
        redirect_uri: redirectUrl,
      }),
    });
    console.log(result);
    if (result.type === 'success') {
      this.handleParams(result.params);
    }
  }

I tried changing the response_type to token id_token but throws an error saying configuration error.

How do I get an id_token?


Solution

  • The token returned is defined by the response_type, not the scope. In the case of the ID token, scope determines what is returned in the token. You need response_type: 'id_token'.

    You can read more about how it works here: https://auth0.com/docs/tokens/id-token#control-the-contents-of-an-id-token

    Full disclosure, I work for Auth0.