简体   繁体   English

创建一个 firebase 云 function 检测 emailVerified 变化

[英]Create a firebase cloud function that detects emailVerified change

I am trying to create a cloud function with firebase functions to detect if the user emailVerified prop changes.我正在尝试使用 firebase 函数创建一个云 function 来检测用户 emailVerified 道具是否发生变化。 This is what I currently have in my index.js in functions folder:这是我目前在函数文件夹中的 index.js 中的内容:

exports.emailVerifiedChange = functions.auth.user().onCreate((user)=> {
    if(user.emailVerified) {
        admin.firestore().collection(`users`).doc(user.uid).create({
            created: new Date(),
            uid: user.uid,
            name: user.displayName,
            searchName: user.displayName.toLowerCase(),
            userInfo: {
              profilePic: '',
              phoneNumber: '',
              email: user.email
            },
            provider: false 
        })
    }
}) 

In the log, this function is called when a user is created but the user collection is not created.在日志中,这个function是在创建用户但没有创建用户集合时调用的。 Any idea?任何的想法? I'm new to functions so not sure why its not working.我是函数的新手,所以不确定为什么它不起作用。

I am trying to create a cloud function with firebase functions to detect if the user emailVerified prop changes.我正在尝试使用 firebase 函数创建一个云 function 来检测用户 emailVerified 道具是否发生变化。

This isn't possible in Cloud Functions, as there's no trigger when the user object changes.这在 Cloud Functions 中是不可能的,因为当用户 object 更改时没有触发器。 The onCreate trigger you use (as its name implies) only triggers when the user is first created.您使用的onCreate触发器(顾名思义)仅在首次创建用户时触发。

Having a trigger when the user profile changes would be a useful addition to Cloud Functions, so I recommend you file feature request for it.在用户配置文件更改时触发将是对 Cloud Functions 的有用补充,因此我建议您为其提交功能请求

The only workaround I can think of for now is to detect the emailVerified status client-side, call a callable or HTTPS Cloud Function with the ID token, verify that ID token , and then store the emailVerified value to Firestore from there.我现在能想到的唯一解决方法是检测客户端的emailVerified状态,使用 ID 令牌调用 callableHTTPS Cloud Function验证 ID token ,然后将emailVerified值从那里存储到 Firestore。

The API reference documentation shows: API 参考文档显示:

firebase.auth().onAuthStateChanged(function(user) {
  if (user) {
    // User is signed in.
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM