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

时间:2020-04-23 14:16:07

标签: javascript firebase vue.js firebase-authentication

我正在使用JavascriptVue并将firebase与我的应用程序集成

场景(已构建)

  • 我有一个sign in页面,用户可以登录
  • 对于要在emailVerified属性中签名的用户,应为

问题

  • 仅当我使用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方法,因为我不再需要它来发送验证邮件

是否仍然可以不登录就发送验证电子邮件?

2 个答案:

答案 0 :(得分:2)

是否仍然可以不登录就发送验证电子邮件?

只能从客户端SDK发送验证电子邮件,并且只有在用户登录后才能发送验证电子邮件。这样做是为了防止滥用Firebase服务器发送垃圾邮件的能力。

如果Firebase的现有电子邮件验证流程不符合您的需求,则您可以实施自己的自定义流程,并在use the Admin SDKs to set the the verification status满足您的验证要求后进行实施。

答案 1 :(得分:1)

您可以使用Cloud Function来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是发送电子邮件的Cloud Function的正式示例。