首页 文章

在Firebase Cloud 功能中创建并返回Firebase电子邮件验证链接

提问于
浏览
1

我有一个firebase Cloud 功能触发器,当有人注册时发送欢迎电子邮件 . 我希望在同一封电子邮件中包含我的电子邮件验证链接,以减少用户在注册时获得的电子邮件数量,并改善入职体验(而不是发送两封单独的电子邮件) .

exports.sendWelcomeEmail = functions.auth.user().onCreate(event => {
  // Get user that signed up
  const user = event.data; // The Firebase user.

  // get the email of the user that signed up
  const email = user.email; // The email of the user.

  // Create email verification link
  var emailVerificationLink = user.createEmailVerificationLink() // NEED HELP HERE: ideally, I would like to create/call a function to create an email verification link for the user here

  // send email
  mailgun.messages().send({
    from: 'support@example.com',
    to: email,
    subject: 'Welcome & Get Started',
    text: 'Welcome! Here are some resources to help you get started, but first verify your email: ' + emailVerificationLink + '!',
    html: // some nice formatted version of the text above
  }, function (error, response) {
    console.log("Email response");
    console.log(response);
    console.log("Email error");
    console.log(error);
  });

})

我仔细查看了documentation on custom email handlers,但似乎他们没有返回电子邮件验证链接,所以我不知道如何在我这里使用这种方法(虽然我希望我错了) .

Is there a way to create the email verification link inside a Firebase Cloud Function in such a way that I could then use resulting link as I please (like in my Welcome email)?

1 回答

  • 1

    没有公共API来获取OOB验证代码或包含该代码的链接 .

    但您可以通过以下几个步骤自行实现:

    • 生成您自己的验证码,您可以安全地存储在某处(例如,在Firebase数据库的受保护部分) .

    • 在链接中将代码嵌入邮件中 .

    • 在该链接上创建 Cloud 功能 .

    • 处理请求,检查数据库中的验证码

    • Set emailVerified to true .

    这与您在调用 sendEmailVerification() 时Firebase身份验证的功能完全不同 .

相关问题