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

标签 node.js firebase react-native firebase-authentication react-native-firebase

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

由于错误(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/

相关文章:

react-native - 如何在 React Native 中编写和建模可重用的业务逻辑

javascript - 基于 JS 中字符串的一部分的数组的不同值

ios - 在 Swift 中优化从 Firestore 获取

google-chrome-extension - Firebase + Chrome内容安全策略设置?

javascript - React Native 中图像顶部的叠加按钮

react-native - Xcode 10 存档构建失败

node.js - Mongoose 在对象展开字段上的聚合查找不起作用

node.js - Socket.io 实时聊天——管理用户好友

javascript - 如何导出分配给变量的要求?

Android FirebaseMessaging Service onMessageReceived 调用了两次