React Native Firebase-Facebook登录未触发onAuthStateChanged侦听器

时间:2020-05-11 00:53:52

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

这是我与RNFB Facebook登录有关的第二个问题。

我遵循以下RNFB提供的official code sample .... code

问题出在行firebase().signInWithCredential(facebookCredential);上。...它没有触发Firebase监听器auth().onAuthStateChanged 所有其他代码均按预期运行,并且facebookCredential变量已正确填充

import { firebase } from '@react-native-firebase/auth';

  onFacebookButtonPress = async () => {
    // Attempt login with permissions
    const result = await LoginManager.logInWithPermissions([
      'public_profile',
      'email',
    ]);

    if (result.isCancelled) {
      throw 'User cancelled the login process';
    }

    // Once signed in, get the users AccesToken
    const data = await AccessToken.getCurrentAccessToken();

    if (!data) {
      throw 'Something went wrong obtaining access token';
    }

    // Create a Firebase credential with the AccessToken
    //const facebookCredential = firebase.FacebookAuthProvider.credential(data.accessToken);
    const facebookCredential = firebase.auth.FacebookAuthProvider.credential(
      data.accessToken,
    );

    // Sign-in the user with the credential
    firebase().signInWithCredential(facebookCredential);
  };

2 个答案:

答案 0 :(得分:0)

我通过在有问题的地方进行尝试/捕获来解决了。原来是因为我曾经使用Google登录。

An account already exists with the same email address but different sign-in credentials. Sign in using a provider associated with this email address

答案 1 :(得分:0)

在添加try / catch之前,您不应该等到出现问题为止。 signInWithCredential返回您处理的许多不同类型的errors。从文档中:

firebase.auth().signInWithCredential(credential).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;
  if (errorCode === 'auth/account-exists-with-different-credential') {
    alert('Email already associated with another account.');
    // Handle account linking here, if using.
  } else {
    console.error(error);
  }
 });

请同时处理其他错误情况?:

auth / account-with-existent-credential

auth / invalid-credential

不允许进行身份验证/操作

auth / user-disabled

等等。

相关问题