【问题标题】:Firebase Re-authentication & email Linking for Phone-Authenticated Users电话认证用户的 Firebase 重新认证和电子邮件链接
【发布时间】:2020-03-07 15:54:54
【问题描述】:

用户注册时使用电话验证。在使用该应用一段时间后,建议他们将一个(电子邮件和密码)链接到他们现有的帐户.

链接过程因错误(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() 我不确定这是否正确。无论如何,它生成了一个错误(如果没有验证证明、会话信息、临时证明或注册 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); 

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

【问题讨论】:

  • 我遇到了同样的问题,但我有一个问题,我使用电话身份验证并在 firebase 控制台中手动设置了一个测试号码所以当我设置其他号码时,我无法接收任何验证码作为短信所以这对你有用吗?
  • @DevAS 我不确定我是否正确理解了您的问题。无论如何,我会尽力回答。我的理解是,您在 Android 模拟器上根本无法接收短信。如果您想尝试接收短信验证码,您需要在实际设备上部署您的应用。这能回答你的问题吗?
  • 也许 ??我想说的是,我有一个登录屏幕,我使用我的实际号码登录,firebase 应该向我发送一条包含验证码的短信,对吗?但是我的手机收不到任何东西,“这是我的应用程序的调试版本,未发布”

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


【解决方案1】:

重要信息:

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-07-17
  • 2019-03-07
  • 1970-01-01
  • 2011-01-14
  • 1970-01-01
  • 2019-05-20
  • 2017-11-06
相关资源
最近更新 更多