简体   繁体   English

当用户更新其 email 时,Firebase

[英]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。

Firebase Function Firebase 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});
});

Deploy部署

$ firebase deploy --only functions:addMessage $ firebase 部署——仅功能:addMessage

Call your Fonction in your app nodejs example firebase v9在您的应用程序 nodejs 示例中调用您的 Fonction firebase v9

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.

相关问题 运行 Cloud Function 将 Firebase 经过身份验证的用户数据存储到 Firestore - Run Cloud Function to store Firebase Auth'ed user data to Firestore 创建与 auth.user 同步的用户文档 onCreate 和 onUpdate Firebase v9 - Create a user document onCreate and onUpdate that is in sync with the auth.user Firebase v9 email未验证时如何注销用户,Firebase Auth - How to sign out the user when the email is not verified, Firebase Auth Firebase Auth,email验证不创建用户? - Firebase Auth, email verification without creating a user? 当用户更新密码时,如何捕获 Firebase Auth 返回的特定错误? - How do I catch specific error returned by Firebase Auth, when user updates their password? firebase.auth(...).signInWithEmailAndPassword 不是云中的 function Function - firebase.auth(...).signInWithEmailAndPassword is not a function in Cloud Function onUpdate 云 function 不适用于 firestore 数据 - onUpdate cloud function is not working with firestore data 当用户使用 firebase 云功能有未读消息/通知时,是否可以向用户发送 email 摘要? - Is there a way to send email digest to a user when they have unread messages/notifications using firebase cloud functions? 如何将Firebase(Cloud Firestore)中的数据库与Laravel Auth用户集成 - How to integrate database in Firebase (Cloud Firestore) with Laravel Auth user 有没有办法判断用户是否在 Firebase Auth 上注册了电话号码或 email 和密码 - Is there a way to tell if a user is registered with phone number or email & password on Firebase Auth
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM