Google使用React Native中的Firebase登录

时间:2017-03-14 17:06:47

标签: firebase react-native firebase-authentication

我正在尝试使用Google登录,但它却抛出了这个错误:

  

代码:“auth / operation-not-supported-in-this-environment”   消息:“此应用程序运行的环境不支持此操作。”location.protocol“必须是http,https或chrome-extension,并且必须启用Web存储。”

这是代码:

const provider = new firebase.auth.GoogleAuthProvider();
provider.addScope('profile');
provider.addScope('email');
firebase.auth().signInWithPopup(provider)
  .then((result) => {
    console.log(result);
  })
  .catch((error) => {
    console.log(error);
  })

附加信息:

  • “firebase”:“^ 3.7.1”
  • “react-native”:“^ 0.42.0”
  • 平台:Android

任何想法? 提前谢谢!

4 个答案:

答案 0 :(得分:3)

首先,我使用react-native-google-signin与Google登录。请按照steps进行配置。

确保您正确sign您的APK(调试或发布)。

app/build.gradle中排除使用它的依赖项中的com.google.android.gms,如下所示:

compile(project(":react-native-google-signin")){
    exclude group: "com.google.android.gms" // very important
}

然后将Google令牌与Firebase链接:

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

我正在使用firebase 3.7.1

这是我的依赖项在app/build.gradle

中的显示方式
dependencies {
    compile project(':react-native-facebook-login')
    compile (project(':react-native-fcm')){
        exclude group: "com.google.firebase"
    }
    compile project(':react-native-vector-icons')
    compile fileTree(dir: "libs", include: ["*.jar"])
    compile "com.android.support:appcompat-v7:23.0.1"
    compile "com.facebook.react:react-native:+"
    compile(project(":react-native-google-signin")){
        exclude group: "com.google.android.gms" // very important
    }
    compile ('com.google.firebase:firebase-core:10.0.1') {
        force = true;
    }
    compile ('com.google.firebase:firebase-messaging:10.0.1') {
        force = true;
    }
    compile ('com.google.android.gms:play-services-auth:10.0.1') {
        force = true;
    }
}

答案 1 :(得分:1)

Firebase Documentation

“{1}},signInWithPopup()signInWithRedirect()linkWithPopup()之类的”头脑“身份验证方法在React Native(或Cordova)中不起作用。您仍然可以使用linkWithRedirect()使用来自您选择的提供商的OAuth令牌登录或与联盟提供商建立链接。

答案 2 :(得分:0)

我尝试了jamesjara所写的内容并且该应用无法构建。在那段确切的代码行上有语法错误。将其更改回上面的原始版本并构建成功。

答案 3 :(得分:0)

我回复很晚但对其他人有帮助,如果你使用带有谷歌标志的firebase那么你必须排除一些库和你的应用程序那样的gradle

 compile(project(":react-native-google-signin")){
    exclude group: "com.google.android.gms" // very important
 }
 compile ('com.google.android.gms:play-services-auth:10.2.6'){
     force = true;
 }
 compile ("com.google.android.gms:play-services-base:+") { // the plus allows you to use the latest version
    force = true;
 }

 compile (project(path: ':react-native-fbsdk')){
            exclude group: "com.google.android.gms"
 }
 compile(project(":react-native-firebase")){
    exclude group: "com.google.android.gms"
 }

This is discussion about that issue for more detail answer