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

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

问题描述

我正在尝试创建一个Cloud Function触发器,该触发器将在验证电子邮件后执行.

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

云函数示例中,我只能找到有关onCreateonDelete.

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 send

      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身份验证模板中的链接更改为在第二步中生成的链接:

        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:

        • 单击编辑图标>单击自定义操作URL:

        • 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.

        我希望我能清楚.

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

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