【问题标题】:Link Multiple Auth Providers to an Account react-native将多个身份验证提供程序链接到一个帐户 react-native
【发布时间】:2018-06-25 10:41:12
【问题描述】:

我是 react-native-firebase 的新手

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

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);
  })

【问题讨论】:

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


    【解决方案1】:

    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)
        })
    }
    

    【讨论】:

    猜你喜欢
    • 2016-10-12
    • 2019-08-17
    • 1970-01-01
    • 2023-03-15
    • 2018-03-09
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    相关资源
    最近更新 更多