gpt4 book ai didi

javascript - Firebase 向用户发送电子邮件验证

转载 作者:行者123 更新时间:2023-12-04 01:28:40 24 4
gpt4 key购买 nike

我正在使用 JavascriptVue并整合firebase用我的应用

场景(已建)

  • 我有一个 sign in用户登录页面
  • 用于登录 emailVerified 的用户属性应该为 true

  • 问题
  • 只有当我使用 firebase.auth().createUserWithEmailAndPassword(email, password) 时,我才能发送验证电子邮件方法

  • 报名方式
    signup: async (email, password) => {
    const user = await firebase.auth().createUserWithEmailAndPassword(email, password)
    await user.user.sendEmailVerification()
    return `Check your email for verification mail before logging in`
    },

    所需解决方案
  • 我从 firebase console 创建一个新用户
  • 我通过email作为参数或uid并且该方法应该向用户发送一封验证电子邮件,以便他们可以验证他们的电子邮件
  • 彻底废弃signup方法,因为我不再需要它来发送验证邮件

  • 有没有在不登录的情况下发送验证电子邮件?

    最佳答案

    您可以使用云函数来 generate an email verification link ,然后通过 Sendgrid、Mailjet 或 Mailgun 等电子邮件微服务或通过您自己的自定义 SMTP 服务器将其发送给用户。

    当使用 functions.auth.user().onCreate() 创建 Firebase 用户时,您将触发此 Cloud Function。事件处理程序。

    由于您将通过 Firebase 控制台创建用户,因此无需用户登录即可触发 Cloud Function。

    沿着这些思路:

    exports.sendEmailVerification = functions.auth.user().onCreate((user) => {

    const email = user.email;

    const url = '...' //Optional, see https://firebase.google.com/docs/auth/custom-email-handler
    const actionCodeSettings = {
    url: url
    };

    // Use the Admin SDK to generate the email verification link.
    return admin.auth().generateEmailVerificationLink(email, actionCodeSettings)
    .then((link) => {
    // Construct email verification template, embed the link and send the email
    // by using custom SMTP server.
    // or a microservice like Sendgrid
    return ...
    })
    .catch((error) => {
    // Some error occurred.
    });

    });

    你会发现 here发送电子邮件的云函数的官方示例。

    关于javascript - Firebase 向用户发送电子邮件验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61389389/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com