本文介绍了无法获得Google OAuth身份验证与React Native和Firebase配合使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让Google OAuth与Firebase和React Native一起工作,似乎无法让所有的东西一起工作。我正在使用NPM软件包来处理谷歌OAuth方面的事情,并按预期工作。我遇到了让用户通过Firebase进行身份验证的问题。



我已经根据Web指南中的说明在我的Firebase实例上启用了Google身份验证。但是,当我尝试使用本地OAuth包中的 idToken 字段进行身份验证时,我总是从Firebase获取 INVALID_CREDENTIALS 错误。我正在使用Firebase Web API中的方法。



我想我已经尝试了使用Firebase配置的Web / Android客户端ID和 idToken 和OAuth包中的 serverAuthCode 字段,但所有内容都以相同的错误结束。有没有人得到这个工作正确?

解决方案

这是我为我的应用程序做的:

 从'react-native-google-signin'导入{GoogleSignin}; 
$ b $ const KEYS = {
// ... // ios和web键(我从我的服务器加载它们)
}

()=> {
GoogleSignin.configure(KEYS)
.then(())$ {
GoogleSignin.hasPlayServices({autoResolve:true})
.then => {
GoogleSignin.signIn()
.then(this.onGoogleLoginSuccess)
.catch(error => {})
.done(); $ b $ ((err)=> {
console.log(播放服务错误,err.code,err.message); $ b $($ b $) b}}
}

GoogleLoginSuccess(用户){
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));

$ / code> $ / pre>

关键是使用 signInWithCredential() code>方法。参考:


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?

解决方案

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

这篇关于无法获得Google OAuth身份验证与React Native和Firebase配合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 08:22