Cloud Functions for Firebase - 验证电子邮件的操作 [英] Cloud Functions for Firebase - action on email verified

查看:16
本文介绍了Cloud Functions for Firebase - 验证电子邮件的操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个将在验证电子邮件后执行的云函数触发器.

Im trying to create a Cloud Function trigger that will execute after email has been verified.

Cloud Functions 示例中,我只能找到有关 onCreate<触发器的示例/code> 和 onDelete.

In the Cloud Functions samples I could only find examples on triggers for onCreate and onDelete.

文档中,我发现了一些关于创建自定义操作处理程序的内容,但是我实际上并不想替换默认情况下的标准电子邮件验证对话框,我只想在验证电子邮件后更改用户"的属性.

Within the documentation I found something about creating custom action handlers but I don't actually want to replace the standard email verification dialog they have by default, I just want to change the property of a "user" after the email is verified.

有没有人有这方面的经验,这甚至可能吗?或者是我创建自定义验证视图/对话框网页的唯一选择?

Does anyone have any experience with this, and is this even possible? Or is my only option to create my custom verification view/dialog webpage?

推荐答案

我遇到了这个问题,花了很长时间才弄清楚如何解决,所以我希望这可以帮助任何可能陷入此问题的人:

I faced this problem and took me a long time to figure it out how to solve so I hope this could help anyone that could get stuck into this too:

1 -> 我为新用户创建了一个由 onCreate() 触发的函数

1 -> I created a function that was triggered with onCreate() for a new user

exports.sendConfirmationEmail = functions.auth.user()
                    .onCreate((user) => {
                        const actionCodeSettings = {
                            url: 'https://appNextURL.com/',
                            handleCodeInApp: false//ensure that the link will open into browser
                        };
                        return admin.auth().generateEmailVerificationLink(user.email, actionCodeSettings)
                            .then(async (link) => {
                                await db.collection('users').doc(user.uid).set({
                                    verificationLink: link,
                                    emailVerified: false
                                }, {merge: true});
                                return sendCustomVerificationEmail(user.email, user.displayName, link);
                            })
                            .catch((err) => {
                                console.error("Error:", err);
                                return Promise.reject(err);
                            });
                    });

  • generateEmailVErificationLink() 将根据我们将在步骤 3 中保存的链接生成链接.

    • The generateEmailVErificationLink() will generate the link based on the link we will save on step 3.

      函数 sendCustomVerificationEmail() 只是一个内部函数,它克服了标准的电子邮件 firebase 发送

      The function sendCustomVerificationEmail() is just an internal function that overcomes the standard email firebase send

      2 -> 然后我创建了一个函数,该函数将接收一个手动 http 触发器,其中包含在发送自动电子邮件时由 firebase 自动生成的数据

      2 -> Then I created a function that will receive a manual http trigger with the data that would be generated automatically by firebase when sending an automatic email

      exports.verifyEmail = functions.https.onRequest((req, res) => {
                              const {mode, oobCode, apiKey, continueUrl, lang} = req.query;
                              const link = "https://us-central1-projectId.cloudfunctions.net/verifyEmail/?mode=" + encodeURIComponent(mode) + "&oobCode=" + encodeURIComponent(oobCode) + "&apiKey=" + encodeURIComponent(apiKey) + "&continueUrl=" + encodeURIComponent(continueUrl) + "&lang=" + encodeURIComponent(lang);
                              return db.collection("users")
                                  .where("verificationLink", "==", link)
                                  .get()
                                  .then(function (querySnapshot) {
                                      querySnapshot.forEach(function (user) {
                                          const userData: UserData = user.data();
                                          console.log("email verified: ", userData.userId);
                                          return admin.auth().updateUser(userData.userId, {
                                              emailVerified: true
                                          }).then(function (userRecord) {
                                              return db.collection('users').doc(userData.userId).set({emailVerified: true}, {merge: true});
                                          });
                                      });
                                      return res.sendStatus(200).end();
                                  }).catch(function (err) {
                                      console.log("error:", err);
                                      return res.sendStatus(403).end();
                                  });
                          });
      

      • 当我在 onCreate() 中保存链接时,我现在可以查询该链接以了解我正在验证的用户是谁
      • 3 -> 第三步是将 Firebase Authentication 模板中的链接更改为第二步中生成的链接:

        3 -> the third step is to change the link in to Firebase Authentication template to the link generated into the 2nd step:

        导航到身份验证>模板:

        Navigate to Authentication>Templates:

        • 点击编辑图标>点击自定义操作网址:

        • Click on edit icon> Click on customize action URL:

        导航

        将生成的链接粘贴到步骤 2 中并保存:

        Paste the link generated into the step 2 and save:

        保存链接

        现在自动生成的每个链接都将使用您在第 2 步中创建的函数,您将能够处理您想要发生的操作.

        Now every link generated automatically will go trought that function you created on step 2 and you will be able to handle the actions you want to happen.

        我希望我能说清楚.

        这篇关于Cloud Functions for Firebase - 验证电子邮件的操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆