2

I'm attempting to get Google OAuth working with Firebase and React Native and can't seem to get everything working together. I'm using the NPM package react-native-google-signin to handle the Google OAuth side of things and that is working as expected. I'm running into an issue getting the user authenticated with Firebase however.

I've enabled Google Authentication on my Firebase instance according to the instructions in the Web Guide. However, when I try authenticating with the idToken field from the native OAuth package I always get an INVALID_CREDENTIALS error from Firebase. I am using the authWithOAuthToken method from the Firebase Web API.

I think I've tried every combination of using the Web/Android client IDs for the Firebase configuration and the idToken and serverAuthCode fields from the OAuth package but everything ends with the same error. Has anyone gotten this to work correctly?

1
  • have you any news about that ? Commented Nov 13, 2016 at 11:24

1 Answer 1

3

This is how i did it for my app:

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 point is to use signInWithCredential() method. Reference: https://firebase.google.com/docs/reference/js/firebase.auth.Auth#signInWithCredential

Sign up to request clarification or add additional context in comments.

1 Comment

Can you tell what is supposed to be printed on .then((data)=>console.log('SUCCESS', data))?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.