【问题标题】:Firebase functions auth.generateEmailVerificationLink() generating link with wrong apiKeyFirebase 函数 auth.generateEmailVerificationLink() 生成带有错误 apiKey 的链接
【发布时间】:2022-11-04 15:08:32
【问题描述】:

我有一个带有开发和产品版本的 Firebase 功能项目。我正在使用 auth.generateEmailVerificationLink() 为新创建的用户发送电子邮件验证。除了在 prod 环境(本地测试或托管)中一切正常,auth.generateEmailVerificationLink() 生成的链接中的 apiKey 与 Firebase 的默认 apiKey 不同。然后单击该链接,我得到带有错误代码的页面:

Try verifying your email again
Your request to verify your email has expired or the link has already been used

请注意,当我获得错误 apiKey 的链接时,如果我将其更改为正确的 apiKey。验证有效。因此,整个问题似乎与生成的电子邮件验证链接中的错误 apiKey 有关。
另请注意,错误的 apiKey 不是随机密钥,而是用于 Google Maps api 的项目前端。

代码本身很简单。 (我省略了创建用户等的代码,因为这些部分都可以正常工作)
- 初始化管理 SDK:

import { initializeApp } from 'firebase-admin/app';
import { getAuth } from 'firebase-admin/auth';

initializeApp();
const auth = getAuth();
export { auth };

- 生成电子邮件验证电子邮件

const sendEmail = async () => {
 const actionCodeSettings = {
   // This url is working correctly, it is the same as in Firebase console
   // and when changing the wrong apiKey to correct redirecting works correctly
   url: process.env.DOMAIN as string,
 };

await auth
  .generateEmailVerificationLink(email, actionCodeSettings) // email is the email of newly created user
  .then((link) => {
     // generate email message with link
     // generate mailOptions
     // use transporter to send email
  });
};

感谢您的任何帮助

编辑
我测试了从 GCP 凭据页面中删除那个“错误的”apiKey 并将其替换为另一个。然后在本地运行该功能一切正常,但“错误”仍在验证电子邮件链接中,即使它不再存在。

Firebase 强烈建议,如果在 Cloud Functions 等中使用 Admin SDK,则应在不带参数的情况下初始化应用程序。
https://firebase.google.com/docs/admin/setup#initialize-without-parameters
对我来说,出于某种原因,似乎有些事情是“错误的”,现在甚至从某个地方删除了 apiKey 以供使用。

【问题讨论】:

    标签: firebase-authentication google-cloud-functions


    【解决方案1】:

    我注意到,与开发项目不同,Web Api Key (Project Settings>General) 与 Web App 的 firebaseConfig apiKey 不同,从而解决了这个问题。
    因此,我在 GCP 凭证中找到了此 Web Api Key(电子邮件验证电子邮件需要身份工具包 API)添加了正确的权限,现在云功能使用正确且有效的 apiKey 发送电子邮件验证电子邮件。

    【讨论】:

      猜你喜欢
      • 2018-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-22
      相关资源
      最近更新 更多