Search code examples
firebasereact-nativefirebase-authenticationexpocreate-react-native-app

Login with google with create-react-native-app and firebase (using exponent)


By this code.

googleAuthenticate = (token) => {
const provider = firebase.auth.GoogleAuthProvider();
const credential = provider.credential(token)
return firebase.auth().signInWithCredential(credential) }


gglogin = async () => {
const LogInResult = await Google.logInAsync({
      androidClientId: '<my-androidClientId>',
      iosClientId: '<my-IOSClientId>',
      scopes: ['profile', 'email']
});

if(LogInResult.type === 'success'){
  Alert.alert(
        'Logged in!',
        `Hi ${user.name}!`,
      );
  this.googleAuthenticate(LogInResult.accessToken)
}

}

It's can get username alert but can't send token to firebase by getting this error.

Possible Unhandled Promise Rejection (id: 0):
  TypeError: this.addScope is not a function. (In 
 'this.addScope("profile")', 'this.addScope' is undefined)`

Please what is wrong


Solution

  • I notice 2 issues, credential is a static method, and the first parameter is an id token and the second an access token (you are passing an access token it seems):

    const credential = firebase.auth.GoogleAuthProvider.credential(null, token);
    return firebase.auth().signInWithCredential(credential);