Unable to get Google OAuth authentication working with React Native and Firebase

假如想象 提交于 2020-01-04 06:17:49

问题


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:


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



来源:https://stackoverflow.com/questions/35877731/unable-to-get-google-oauth-authentication-working-with-react-native-and-firebase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!