React-native-auth0 saveCredentials error - Cannot read property 'hasValidAuth0Instance' of null

I have a React Native v0.75.4 application built with the React Native CLI where I’m implementing the passwordRealm login method then saving credentials using the credentialsManager.

The passwordRealm call is successful and returns a credentials object, but when trying to save it using the credentialsManager saveCredentials method, it throws the following error:

[a0.credential_manager.invalid: Cannot read property 'hasValidAuth0Instance' of null]

The version of the react-native-auth0 SDK in package.json is:
"react-native-auth0": "^3.2.1"

The code in my application is below:

import Auth0 from 'react-native-auth0';

const auth0 = new Auth0({
  domain: DOMAIN,     // uses real value
  clientId: CLIENT_ID,  // uses real value
});

export const login = async (username: string, password: string) => {
  try {
    const credentials = await auth0.auth.passwordRealm({
      username,
      password,
      realm: config.realm,
      scope: config.scope,
    });
    await auth0.credentialsManager.saveCredentials(credentials);  <- error
  } catch (err) {
    console.log(err);
  }
};

Hi @israataha

Welcome to the Auth0 Community!

I am sorry about the late reply to your inquiry.

As far as I have checked, the hasValidAuth0Instance error that you are receiving seems to be specific when you are using Expo Go with your React Native Application.

As mentioned on our SDK page:

This SDK is not compatible with “Expo Go” app. It is compatible only with Custom Dev Client and EAS builds.

You can also try to change the iOS target deployment version to 13.0 in the podfile if you are using an iOS environment/simulation.

Alternatively, you might be missing the audience parameter in the authorize() call. You would have to pass the audience parameter when you make the call as follows:

const onLogin = async () => {
    try {
      await authorize({ audience: 'https://test.api'});
      let credentials = await getCredentials();
      Alert.alert('AccessToken: ' + credentials.accessToken);
    } catch (e) {
      console.log(e);
    }
  };

This code is referenced here: react-native-auth0/src/hooks/tests/use-auth0.spec.jsx at e1771491969934cc20dd3ed0e8beeb6c5cf70577 · auth0/react-native-auth0 · GitHub

If you have any other questions on the matter or found a solution to it already, feel free to let us know by leaving a reply!

Kind Regards,
Nik

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