当用户节点更新时,是否有一种方法可以触发Firebase功能?

编程入门 行业动态 更新时间:2024-10-28 14:32:07
本文介绍了当用户节点更新时,是否有一种方法可以触发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做到这一点.请参见 firebase.google/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 firebase.google/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功能?

本文发布于:2023-11-06 15:56:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1564140.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有一   种方法   节点   功能   用户

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!