gpt4 book ai didi

node.js - 针对电话验证用户的 Firebase 重新身份验证和电子邮件链接

转载 作者:太空宇宙 更新时间:2023-11-03 22:19:52 24 4
gpt4 key购买 nike

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

由于错误(auth/requires-recent-login),链接过程失败。我的代码如下。

// 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,则无法创建 PhoneAuthCredntial。)。

我的代码如下。

// 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) 需要属性凭据
  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);

关于node.js - 针对电话验证用户的 Firebase 重新身份验证和电子邮件链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58810213/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com