androidreact-nativereact-native-iosgoogle-signinreact-native-firebase

React Native Google Signin with multiple accounts


I have a react native application which uses google sign in. currently, I can sign in with one of my google accounts which my mobile signed in, but I need to do an OAuth for another google account in my google accounts stack, this is to get the API access I need the serverAuthToken for the secondary accounts

The library I'm using for google login - https://github.com/react-native-community/google-signin

What I have done till now:

const firebaseGoogleLogin = async () => {
    try {
      await GoogleSignin.hasPlayServices();
      const userInfo = await GoogleSignin.signIn();
      if (userInfo) {
        await storeToken(userInfo);
        // eslint-disable-next-line no-console
        console.log('User info retrieved during login', userInfo);
      }
      // create a new firebase credential with the token
      // eslint-disable-next-line max-len
      const credential = firebase.auth.GoogleAuthProvider.credential(userInfo.idToken, userInfo.accessToken);
      // login with credential
      // eslint-disable-next-line no-unused-vars
      const firebaseUserCredential = await firebase.auth().signInWithCredential(credential);
      console.log('Logged in')
    } catch (error) {
      if (error.code === statusCodes.SIGN_IN_CANCELLED) {
        console.log('sign in cancelled', error);
      } else if (error.code === statusCodes.IN_PROGRESS) {
        console.log('signin in progress error', error);
      } else if (error.code === statusCodes.PLAY_SERVICES_NOT_AVAILABLE) {
        console.log('play services are not available or outdated', error);
      } else {
        console.log('some other error occurred', error);
      }
    }
  };

Result for the above code:

This is the code I used to login with my primary Google account, after logging in I'm triggering the play services modal to sign in with other accounts using the above, but the modal closes with loading a sec and gets closed, shows me Logged in in the log, since I have already logged, in the play services automatically bypass the option to select an account

The above problem occurs only in android, ios seems fine even after logging in with an account

Was trying to do a hack if there isn't a way for multiple account authorization since an account already signed in it is bypassing the play service modal, I'm thinking to logout the user once he signed into my application (process level) and inside the main activity, I can have the play services still? is this the right way to do!


Solution

  • Probably you have fixed the issue. I think the method below will solve your problem. Call this after logout.

        signOut = async () => {
          try {
            await GoogleSignin.revokeAccess();
            await GoogleSignin.signOut();
            this.setState({ user: null }); // Remember to remove the user from your app's state as well
          } catch (error) {
            console.error(error);
          }
        };