当用户节点更新时,是否有一种方法可以触发Firebase功能? [英] Is there a way to trigger a Firebase Function when the user node is updated?

本文介绍了当用户节点更新时,是否有一种方法可以触发Firebase功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个包含用户关联电子邮件的节点.每当用户重置其身份验证电子邮件时,都会通过扇出技术在Firebase身份验证用户节点以及数据库中的两个其他节点上对其进行更新.用户每次更新身份验证电子邮件时,都会收到一封电子邮件地址更改通知,该通知使他们可以还原电子邮件地址更改.

I have two nodes that contain a user's associated email. Whenever the user resets their authentication email, it is updated in the Firebase Authentication user node, as well as two additional nodes in my database via a fan-out technique. Each time a user updates their Authentication email they're sent an email address change notification which allows them to revert the email address change.

我遇到的问题是,如果用户还原这些更改,则正确的电子邮件地址将不再反映在数据库中,并且会保持不一致状态.我的解决方案是每当用户更改身份验证电子邮件时,通过Cloud Function自动更新这些节点.

The problem I am running into is that if a user reverts these changes, the proper email address is no longer reflected in the database and it's left in an inconsistent state. My solution would be to have these nodes automatically updated via Cloud Function whenever a user changes their authentication email.

这可能吗?如果是这样,我将用什么来实现它?如果没有,还有其他人知道的解决方法,可以使我的数据库保持一致状态以进行身份​​验证电子邮件更改吗?

Is this possible? If so, what would I use to implement it? If not, is there another workaround that anyone knows of to keep my database in a consistent state for Authentication email changes?

推荐答案

经过几个小时的侦查,我发现可以通过Firebase Admin SDK做到这一点.请参见 https://firebase.google.com/docs/auth/admin/manage -用户以获取更多详细信息.

After quite a few hours of sleuthing, I have figured out that this is possible through the Firebase Admin SDK. See https://firebase.google.com/docs/auth/admin/manage-users for more details.

基本上,您将创建一个Cloud Function,该Cloud Function使用admin SDK重置电子邮件,而不会向用户发送该讨厌的通知,并且成功后,将使用服务器端扇出来更新数据库.

Basically, you make a Cloud Function which uses the admin SDK to reset the email without sending that pesky notification to the user and, on success, uses sever-side fan out to update the database.

例如:

const functions = require('firebase-functions');
const admin = require("firebase-admin");
// Initializes app when using Firebase Cloud Functions 
admin.initializeApp(functions.config().firebase);
const databaseRef = admin.database().ref();


exports.updateEmail = functions.https.onRequest((request, response) 
    // Cross-origin headers
    response.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, OPTIONS");
    response.setHeader("Access-Control-Allow-Origin", "YOUR-SITE-URL");
    response.setHeader("Access-Control-Allow-Headers", "Content-Type");

    const email = request.body.email;
    const uid = request.body.uid;

    // Update auth user
    admin.auth().updateUser(uid, {
        "email": email
    })
    .then(function() {
        // Update database nodes on success

        let fanoutObj = {};
        fanoutObj["/node1/" + uid + "/email/"] = email;
        fanoutObj["/node2/" + uid + "/email/"] = email;

        // Update the nodes in the database
        databaseRef.update(fanoutObj).then(function() {
            // Success
            response.send("Successfully updated email.");
        }).catch(function(error) {
            // Error
            console.log(error.message);
            // TODO: Roll back user email update
            response.send("Error updating email: " + error.message);
        });
    })
    .catch(function(error) {
        console.log(error.message);
        response.send("Error updating email: " + error.message);
    });
});

这种技术可用于在您以后必须执行某些任务的情况下进行用户信息更改,因为Firebase尚不具备在用户的个人资料数据更改时运行的Cloud Function触发器,如Doug Stevenson在评论.

This technique can be used to do user information changes in cases where you have to perform some task afterwards, since Firebase does not yet have a Cloud Function trigger which runs when a user's profile data changes, as noted by Doug Stevenson in the comments.

这篇关于当用户节点更新时,是否有一种方法可以触发Firebase功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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