2017-10-16 109 views
0

我正在嘗試將我的電話號碼與我的電子郵件密碼身份驗證相關聯。所以我建立我的註冊使用以下步驟:Firebase電話身份驗證和鏈接

  1. 用戶輸入電子郵件地址和密碼。
  2. 然後我打電話firebase.auth().createUserWithEmailAndPassword(values.email, values.password)
  3. 然後我需要經常賬戶與手機號碼聯繫起來,所以我使用firebase.auth().currentUser.linkWithPhoneNumber("+xxxxxxxxxx", xxx)

不過,我看不出有任何的連接。 2個帳戶在我的Firebase控制檯中創建,當前用戶只在他的詳細信息中包含電話號碼。當我再次用電子郵件和密碼登錄並檢查用戶的詳細信息時,電話號碼不存在!

請在下面找到我的代碼:

onSubmit(values) { 
    this.props.firebase.auth().createUserWithEmailAndPassword(values.email, values.password).then((user) => { 
     //send recaptchaverifier 
     window.recaptchaVerifier.verify(); 

    }).catch((error) => { 
     console.log(error); 
    }); 
} 


window.recaptchaVerifier = new firebase.auth.RecaptchaVerifier('submit-button', { 
     'size': 'invisible', 
     'callback': function(response){ 
     //called when we call "window.recaptchaverifier.verify() in 
     //onSubmit function 
      var xxx = window.recaptchaVerifier; 

      this.props.firebase.auth().currentUser.linkWithPhoneNumber("+xxxxxxxx", xxx) 
       .then((verificationId) => { 
        console.log('inside'); 
        console.log(resp); 
        var verificationCode = window.prompt('Please enter the verification ' + 
         'code that was sent to your mobile device.'); 
        return firebase.auth.PhoneAuthProvider.credential(resp.verificationId, 
         verificationCode); 
       }).then((phoneCredential) => { 
       console.log("RESULT OF AUTH", phoneCredential); 
       console.log("USER INFO: ", this.props.firebase.auth().currentUser); 
       return this.props.firebase.auth().signInWithCredential(phoneCredential) 
      }).catch((error) => { 
       console.log("ERRORS: ", error); 
      }).catch((error) => { 
       console.log("ERROR", error) 
      }); 
     }.bind(this) 
    }); 

回答

0

您正在使用它創建一個新用戶的手機通話憑證signInWithCredential。您需要執行以下操作:

firebase.auth().currentUser.linkWithPhoneNumber("+xxxxxxxx", xxx) 
    .then((confirmationResult) => { 
    // At this point SMS is sent. Ask user for code. 
    let code = window.prompt('Please enter the 6 digit code'); 
    return confirmationResult.confirm(code); 
    }) 
    .then((result) { 
    // Phone credential now linked to current user. 
    // User now can sign in with email/pass or phone. 
    }); 
    .catch((error) => { 
    // Error occurred. 
    });