【问题标题】:Firebase send email verification to userFirebase 向用户发送电子邮件验证
【发布时间】:2020-08-06 21:44:52
【问题描述】:

我正在使用 Javascript 和 Vue 并将 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 方法,因为我不再需要它来发送验证邮件了

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

【问题讨论】:

    标签: javascript firebase vue.js firebase-authentication


    【解决方案1】:

    您可以使用云函数到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 是发送电子邮件的云函数的官方示例。

    【讨论】:

      【解决方案2】:

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

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

      如果 Firebase 的现有电子邮件验证流程不符合您的需求,您可以实施您自己的自定义流程和 use the Admin SDKs to set the the verification status,一旦它们满足您的验证要求。

      【讨论】:

        猜你喜欢
        • 2018-08-14
        • 2019-06-24
        • 2018-11-21
        • 1970-01-01
        • 2021-05-12
        • 2019-10-29
        • 2017-09-09
        • 2014-09-18
        • 1970-01-01
        相关资源
        最近更新 更多