Search code examples
react-nativeoauth-2.0expo

How to get a new access token via refresh token using Expo Auth Session?


I'm using Google for auth (expo-auth-session/providers/google)

I can successfully login for the first time and fetch an access token and a refresh token. The refresh token will get stored in SecureStorage.

Now at this point, when the old access token is invalidated, I need to use the refresh token to get a new access token, but Expo's docs don't really provide any guidance on this part. I have checked their API quite thoroughly, but can't see anything that helps me retrieve a new access token with a refresh token.

Any guidance would be welcome.


Solution

  • The AuthSession library has a method specifically for refreshing tokens. It requires the clientId used to retrieve the token initially, so you can reuse that, the refreshToken which you have stored as well as a token endpoint.

    const tokenResult = await AuthSession.refreshAsync({
              clientId: "<your-client-id>>",
              refreshToken: "<your-refresh-token>",
            }, {
              tokenEndpoint: "www.googleapis.com/oauth2/v4/token",
            },
          );
    

    I wasn't able to test this myself as we use firebase and useIdTokenAuthRequest as a result, so I wasn't able to get my hands on a refreshToken to run it through that function - but the documentation of that method seems pretty solid.