将多个Auth Provider链接到一个本机帐户

时间:2018-06-25 10:41:13

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

我是react-native-firebase的新手

我想在登录后与Facebook提供者和google提供者链接用户 我尝试了Internet上的所有解决方案,但其中任何一个都有效。 这是我的代码

const loginUser = await firebase.auth().signInAndRetrieveDataWithEmailAndPassword('test@gmail.com','password888').then(async function(userRecord) {
    console.log("Successfully sign in user:", userRecord.user._user);
    let user = firebase.auth().currentUser;
    console.log('current user ',user)
    let linkAndRetrieveDataWithCredential=firebase.auth().currentUser.linkAndRetrieveDataWithCredential(firebase.auth.FacebookAuthProvider.PROVIDER_ID).then(async u=>{
    console.log('linkAndRetrieveDataWithCredential u',u)

    }).catch(async (e)=>{
    console.log('linkAndRetrieveDataWithCredential error',e)

    })
    console.log('linkAndRetrieveDataWithCredential error',linkAndRetrieveDataWithCredential)
    /**/
    await firebase.auth().fetchSignInMethodsForEmail('sss@sss.sss')
    .then(async providers => {
      console.log('login index providers',providers)
    }).catch(function(error){
      console.log('login index providers error',error)
    })
  }).catch(async function(error){
    console.log('login error',error,error.email)
    if(error.code=='auth/user-not-found'){
    }else if(error.code=='auth/wrong-password'){
      errorMsg=`${L('password')} ${L('notValid')}`
    }
    if(errorMsg){
      if (Platform.OS === 'android') {
        ToastAndroid.show(
          errorMsg,
          ToastAndroid.LONG
        )
      } else {
        Alert.alert(
          '',
          errorMsg,
          [{ text: L('close'), style: 'cancel' }]
        )
      }
    }
    console.log("Error sign in user:", error.code);
  })

1 个答案:

答案 0 :(得分:0)

linkAndRetrieveDataWithCredential需要AuthCredential,在我的应用中,我使用react-native-fbsdk来获取证书(您需要遵循其设置说明)。 此功能将提示用户登录其Facebook帐户并返回AccessToken,然后您从firebase中获得凭据,最后是linkAndRetrieveDataWithCredential

linkToFacebook = () => {
  LoginManager.logInWithReadPermissions(['public_profile', 'email'])
    .then((result) => {
      if (result.isCancelled) {
        return Promise.reject(new Error('The user cancelled the request'))
      }
      // Retrieve the access token
      return AccessToken.getCurrentAccessToken()
    })
    .then((data) => {
      // Create a new Firebase credential with the token
      const credential = firebase.auth.FacebookAuthProvider.credential(data.accessToken)

      // Link using the credential
      return firebase.auth().currentUser.linkAndRetrieveDataWithCredential(credential)
    })
    .catch((error) => {
      const { code, message } = error
      window.alert(message)
    })
}