无法使用 Firebase 中的电子邮件链接重新进行身份验证

The*_*722 6 javascript firebase firebase-authentication

我的网络应用程序中有电子邮件链接登录设置,并且运行良好。但是,我需要在执行特定操作之前重新验证用户身份。

我正在使用以下代码重新进行身份验证:

let credential = firebase.auth.EmailAuthProvider.credentialWithLink(userEmail, window.location.href);

firebase.auth().currentUser.reauthenticateWithCredential(credential).then(function (usercred: any) {
     console.log('Reauthentication test successful.');
}).catch(function (error: any) {
     console.log('Reauthentication test failed.');
});
Run Code Online (Sandbox Code Playgroud)

但是,凭证每次都会抛出错误:

code: "auth/argument-error"
message: "Invalid email link!"
Run Code Online (Sandbox Code Playgroud)

即使我尝试更改 URL,情况也是如此。该 url 在控制台中获得授权,并且是 https(我最初在 localhost 上进行测试,认为这可能是问题所在)。

有任何想法吗?

ble*_*her 2

我遇到了同样的问题,并且文档在此过程中有点模糊。我不想使用已经存在的 UI 解决方案 (),所以我必须弄清楚。

这是文档片段:

import { getAuth, linkWithCredential, EmailAuthProvider } from "firebase/auth";

// Construct the email link credential from the current URL.
const credential = EmailAuthProvider.credentialWithLink(
  email, window.location.href);

// Link the credential to the current user.
const auth = getAuth();
linkWithCredential(auth.currentUser, credential)
  .then((usercred) => {
    // The provider is now successfully linked.
    // The phone user can now sign in with their phone number or email.
  })
  .catch((error) => {
    // Some error occurred.
  });

Run Code Online (Sandbox Code Playgroud)

该代码window.location.href假设您正在通过电子邮件链接登录打开链接

它与其他电子邮件链接过程没有太大不同,您必须触发从客户端或服务器发送的电子邮件。

我会让页面来处理其他模式(密码重置,...),并且还有这个特定的逻辑来处理新的身份验证或只是在页面加载时重新进行身份验证。

这是基于文档的 Angular (Typescript) 示例

import { getAuth, linkWithCredential, EmailAuthProvider } from "firebase/auth";

// Construct the email link credential from the current URL.
const credential = EmailAuthProvider.credentialWithLink(
  email, window.location.href);

// Link the credential to the current user.
const auth = getAuth();
linkWithCredential(auth.currentUser, credential)
  .then((usercred) => {
    // The provider is now successfully linked.
    // The phone user can now sign in with their phone number or email.
  })
  .catch((error) => {
    // Some error occurred.
  });

Run Code Online (Sandbox Code Playgroud)