Unable to get Google OAuth authentication using React Native and Firebase

I am trying to get Google OAuth to work with Firebase and React Native and cannot get everything to work together. I use the NPM package react-native-google-signin to handle the Google OAuth side and work as expected. I ran into the problem of getting a user using Firebase.

I have enabled Google Authentication in my Firebase instance as instructed in the web guide. However, when I try to authenticate with a field idTokenfrom my own OAuth package, I always get an error INVALID_CREDENTIALSfrom Firebase. I am using the authWithOAuthToken method from the Firebase web interface.

I think I tried every combination of using Web / Android client IDs to configure Firebase and fields idTokenand serverAuthCodefrom the OAuth package, but it all ends with the same error. Has anyone got this to work correctly?

+2
source share
1 answer

Here is how I did it for my application:

import {GoogleSignin} from 'react-native-google-signin';

const KEYS = {
    // ... // ios and web keys (i'm loading them from my server)
} 

login() {
  GoogleSignin.hasPlayServices({ autoResolve: true })
   .then(() => {
      GoogleSignin.configure(KEYS)
        .then(() => {
          GoogleSignin.signIn()
            .then(this.onGoogleLoginSuccess)
            .catch(error=>{})
            .done();
        });
   })
   .catch((err) => {
     console.log("Play services error", err.code, err.message);
   })
}

onGoogleLoginSuccess(user) {
  var token = user.idToken;
  var provider = firebase.auth.GoogleAuthProvider;
  var credential = provider.credential(token);
  firebase.auth().signInWithCredential(credential)
    .then((data)=>console.log('SUCCESS', data))
    .catch((error)=>console.log('ERROR', error));
}

The goal is to use the method signInWithCredential(). Link: https://firebase.google.com/docs/reference/js/firebase.auth.Auth#signInWithCredential

+2
source

Source: https://habr.com/ru/post/1672283/


All Articles