带有Expo的Google Firebase身份验证未返回任何内容

时间:2019-03-09 11:53:41

标签: javascript firebase react-native firebase-authentication expo

我正在创建一个博览会应用程序,用户可以在其中使用Gmail登录。

我遵循this firebase documentation来实现该功能,但是每当我单击登录名时,它既不将数据保存在数据库中也不返回任何错误。

这是我的firebase函数:

isUserEqual = (googleUser, firebaseUser)=> {
  if (firebaseUser) {
    var providerData = firebaseUser.providerData;
    for (var i = 0; i < providerData.length; i++) {
      if (providerData[i].providerId === firebase.auth.GoogleAuthProvider.PROVIDER_ID &&
          providerData[i].uid === googleUser.getBasicProfile().getId()) {
        // We don't need to reauth the Firebase connection.
        return true;
      }
    }
  }
  return false;
}

onSignIn = (googleUser)=> {
  console.log('Google Auth Response', googleUser);
  // We need to register an Observer on Firebase Auth to make sure auth is initialized.
  var unsubscribe = firebase
  .auth()
  .onAuthStateChanged(function(firebaseUser) {
    unsubscribe();
    // Check if we are already signed-in Firebase with the correct user.
    if (!this.isUserEqual(googleUser, firebaseUser)) {
      // Build Firebase credential with the Google ID token.
      var credential = firebase.auth.GoogleAuthProvider.credential(
          googleUser.idToken,
          googleUser.accessToken
          );
      // Sign in with credential from the Google user.
      firebase.auth()
      .signInAndRetrieveDataWithCredential(credential)
      .then(function(result) {
        console.log('User signed in');
      })
      .catch(function(error) {
        // Handle Errors here.
        var errorCode = error.code;
        var errorMessage = error.message;
        // The email of the user's account used.
        var email = error.email;
        // The firebase.auth.AuthCredential type that was used.
        var credential = error.credential;
        // ...
      });
    } else {
      console.log('User already signed-in Firebase.');
    }
  }.bind(this)
  );
};

signInWithGoogleAsync = async() => {
  try {
    const result = await Expo.Google.logInAsync({
      behavior: 'web',
      androidClientId: '929952027781-5ao9pp7n5n0sj2n70i5tp7klfro88bgp.apps.googleusercontent.com',
      iosClientId: '929952027781-7obs66o3kr59kdhp6ll0c9598ue3u8aa.apps.googleusercontent.com',
      scopes: ['profile', 'email'],
    });

    if (result.type === 'success') {
      this.onSignIn(result);
      return result.accessToken;
    } else {
      return {cancelled: true};
    }
  } catch(e) {
    return {error: true};
  }
}

这是我的登录按钮:

<TouchableOpacity style={styles.AuthOptionGmail}  onPress={() => signInWithGoogleAsync()}>
          <Ionicons color='#ffffff' style = {styles.BtnIcon} name="logo-google" size={25}/>
          <Text style={{fontSize:16,color:'#ffffff', textAlign:'center'}}>Login with Gmail</Text>
        </TouchableOpacity>

有人可以告诉我我在哪里搞砸吗??

1 个答案:

答案 0 :(得分:0)

使用Expo sdk 32及更高版本对我有效,只需安装“ expo-google-app-auth”

import * as Google from "expo-google-app-auth";

     signInWithGoogleAsync = async () => {
        console.log("signInWithGoogleAsync");
        try {
          //clientId
          const { type, accessToken, user, idToken } = await Google.logInAsync({
            behavior: "web",
            androidClientId:
              "your id",
            iosClientId:
              "your id",
            scopes: ["profile", "email"]
          });

          if (type === "success") {
            console.log("accessToken" + accessToken);
            console.log("idToken" + idToken);
            console.log(user);
            return accessToken;
          } else {
            return { cancelled: true };
          }
        } catch (e) {
          console.log(e);
          return { error: true };
        }
      };