针对经过电话身份验证的用户的 Firebase 重新身份验证和电子邮件链接



当用户注册时,他们使用电话身份验证使用该应用程序一段时间后,建议他们将(电子邮件和密码(链接到现有帐户。

由于以下错误(auth/要求最近登录(,链接过程失败。我的代码如下。

// The following generates the error: [auth/requires-recent-login] This operation is sensitive and requires recent authentication. Log in again before retrying this request.
const emailCredential = firebase.auth.EmailAuthProvider.credential(state.email, state.password);
const newCredential = await firebase.auth().currentUser.linkWithCredential(emailCredential); 

为了修复这个错误,我知道在链接之前需要调用reauthenticateWithCredential((。但是,我不想要求用户再次登录(接收并输入验证码。(这可能吗?

我尝试将currentUser.getIdToken(true)的结果传递给PhoneAuthProvider.credential(),我不确定这是否正确。无论如何,它生成了一个错误(在没有verificationProof、sessionInfo、临时证明或注册ID的情况下无法创建PhoneAuthCredential(。

我的代码如下。

// The following works: 
const accessToken = await firebase.auth().currentUser.getIdToken(true); 
// The following works: 
const currentCredential = firebase.auth.PhoneAuthProvider.credential(accessToken); 
// The following generates the error: Cannot create PhoneAuthCredential without either verificationProof, sessionInfo, temporary proof, or enrollment ID.
const abc = await firebase.auth().currentUser.reauthenticateWithCredential(currentCredential); 
// The following is never reached:  
const emailCredential = firebase.auth.EmailAuthProvider.credential(state.email, state.password);
const newCredential = await firebase.auth().currentUser.linkWithCredential(emailCredential); 

感谢您的努力和时间来帮助我…

重要信息:

  1. firebase.auth((.currentUser.reauthenticateWithCredential(credential(需要属性credential
  2. 对于使用电话号码登录的用户,我找不到在需要时获取此凭据的方法顺便说一句,对于使用其他提供商(如Facebook(登录的用户,也可以获得它
  3. 但是,对于使用电话号码登录的用户,可以在登录过程中获得此凭据检查https://firebase.google.com/docs/auth/web/phone-auth.
  4. 因此,我决定在登录过程中将用户的凭据保存在他们的设备上。我正在使用Redux和Redux-Persist

修复后的我的代码。

// This is an extract from the login script: 
firebase.auth().signInWithPhoneNumber(phoneNo)
.then(confirmResult => {
dispatch({ type: "PhoneNo_accepted", payload: { confirmResult: confirmResult } }); 
})
.catch(error => { 
dispatch({ type: "display_message", payload: { messageText: `Phone Number Error: ${error.message}` } });
});
// Change#1. The following statement is a NEW step, which I added to get the credential during the login process. 
const credential = firebase.auth.PhoneAuthProvider.credential(state.confirmResult.verificationId, state.codeInput);
state.confirmResult.confirm(state.codeInput)
.then( (user) => {
// Change#2. The following function would save the credential to the app's state, e.g. using Redux
_onAuthComplete(user, credential);
})
.catch( error => { 
dispatch({ type: "display_message", payload: { messageText: `Verification Code Error: ${error.message}` } });
});
// // // 
// This is an extract from the linking script: 
// Change#3. props.credential is the credential, which was saved to the app's state. 
await firebase.auth().currentUser.reauthenticateWithCredential(props.credential);
const emailCredential = firebase.auth.EmailAuthProvider.credential(state.email, state.password);
const newCredential = await firebase.auth().currentUser.linkWithCredential(emailCredential); 

最新更新