[英]Firebase auth onUpdate cloud function for when a user updates their email
I need to run a Firebase function whenever my user updates their email address, except auth only has onCreate and onDelete.每当我的用户更新他们的 email 地址时,我都需要运行 Firebase function,除了 auth 只有 onCreate 和 onDelete。 How can I react to email updates?我如何对 email 更新做出反应?
It's not possible today to directly react to an email address changing in Firebase Authentication.现在无法直接对 Firebase 身份验证中更改的电子邮件地址做出反应。 If you'd like to see that as a feature, please file a feature request .如果您希望将其视为一项功能,请提交功能请求。
You can react to it indirectly by having your app listen to authentication events ( Android ), take the User object delivered to your listener, and write the user's email address to a RealtimeDatabase location (or Firestore document) for that user's UID.您可以通过让您的应用程序侦听身份验证事件 ( Android )、将User 对象传递给您的侦听器并将用户的电子邮件地址写入该用户的 UID 的 RealtimeDatabase 位置(或 Firestore 文档)来间接对其做出反应。 Then, you can have a database trigger that tracks the location of your users in the database, and react to the change there.然后,您可以拥有一个数据库触发器来跟踪您的用户在数据库中的位置,并对那里的更改做出反应。
正如 Doug Stevenson 所说,此功能尚未实现,因此您可以在 updatePhoneNimber 回调之后使用 Firestore 文档更新,然后在您的用户集合上使用 OnUpdate 或 OnWrite 触发此功能,例如通知您自己的后端服务器
"My Workaround Use functions.https.onCall to create an HTTPS callable function.我的解决方法使用 functions.https.onCall 创建一个 HTTPS 可调用 function。
exports.onUpdateUserEmail = functions.https.onCall((data, context) => {
const uid = context.auth.uid;
const email = context.auth.token.email || null;
return admin.
firestore()
.collection("users")
.doc(uid)
.set({email});
});
$ firebase deploy --only functions:addMessage $ firebase 部署——仅功能:addMessage
import { initializeApp } from 'firebase/app';
import { getAuth, updateEmail } from "firebase/auth";
import { getFunctions, httpsCallable } from 'firebase/functions';
const app = initializeApp({
projectId: '### CLOUD FUNCTIONS PROJECT ID ###',
apiKey: '### FIREBASE API KEY ###',
authDomain: '### FIREBASE AUTH DOMAIN ###',
});
const functions = getFunctions(app);
const onUpdateEmail = httpsCallable(functions, 'onUpdateUserEmail');
const auth = getAuth();
updateEmail(auth.currentUser, "user@example.com").then(() => {
// Email updated!
onUpdateEmail();
}).catch((error) => {
// An error occurred
// ...
});
If you're working with Firestore or RTDB, you could do the opposite trigger.如果您使用 Firestore 或 RTDB,则可以执行相反的触发器。 When a user update's their "photoURL" in a users
collection in Firestore, for example, it triggers a profile update in Firebase Auth.例如,当用户在 Firestore 的users
集合中更新其“photoURL”时,它会触发 Firebase Auth 中的配置文件更新。
This does not prevent the profile from being updated indirectly, but it does give you only one command to update.这不会阻止配置文件被间接更新,但它确实只为您提供了一个更新命令。
Maybe one day there will be Firestore Auth Rules.也许有一天会有 Firestore 身份验证规则。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.