简体   繁体   English

如何编写一个云 function,它在新用户通过身份验证后在 Firestore 数据库中创建一个用户?

[英]How do I write a cloud function which creates a user in the Firestore database once a new user gets authenticated?

So far I have written the following code:到目前为止,我已经编写了以下代码:

const functions = require("firebase-functions");
const admin = require("firebase-admin");

admin.initializeApp(functions.config().firebase);

exports.addAccount = functions.auth.user().onCreate((event) => {
  const user = event.data; // The firebase user
  const id = user.uid;

  return admin
    .database()
    .ref("/users/" + id)
    .set("ok");
});

The idea here is to execute a cloud function once a new user gets created.这里的想法是在创建新用户后执行云 function。 As this happens, I want to create a new node in the Firestore database.发生这种情况时,我想在 Firestore 数据库中创建一个新节点。 Under the 'users' collection I want to add a document with the uid, which contains a bit of information, in this case a String that says 'ok'.在“用户”集合下,我想添加一个带有 uid 的文档,其中包含一些信息,在本例中是一个表示“确定”的字符串。

When deploying this function it produces an error.部署此 function 时会产生错误。 How exactly should I go about creating it?我 go 究竟应该如何创建呢?

The admin.database() returns the Realtime Database service. admin.database()返回实时数据库服务。 To use Firestore, use firebase.firestore() .要使用 Firestore,请使用firebase.firestore() Try refactoring the code as shown below:尝试重构代码,如下所示:

exports.addAccount = functions.auth.user().onCreate((event) => {
  const user = event.data; // The firebase user
  const id = user.uid;

  return admin
    .firestore()
    .collection("users")
    .doc(id)
    .set({ uid: id, ...user });
});

暂无
暂无

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

相关问题 如何将Firebase(Cloud Firestore)中的数据库与Laravel Auth用户集成 - How to integrate database in Firebase (Cloud Firestore) with Laravel Auth user Firestore 新数据库 - 如何备份 - Firestore new database - How do I backup 使用 Cloud Functions 访问 firestore 中的用户数据,但如何安全地发送 UID/IDToken? - Accessing user data in firestore using Cloud Functions, but how do I securely send the UID/IDToken? 如何使用用户输入的文档名称在 firestore 中输入数据库? - How do i input database in firestore with user's input for name of the document? Firestore 安全规则缺少为新的经过身份验证的用户创建新文档的权限 - Firestore security rules missing permissions on create new document for new authenticated user Cloud FireStore 多个文档具有相同的 iD,即用户 UID - Cloud FireStore multiple document with same iD which is the user UID 如何在 Flutter 中创建新用户后将默认用户名、主题模式和主题颜色存储到 Cloud Firestore? - How to store default userName, themeMode and themeColor to Cloud Firestore after creating new user in Flutter? 如何将用户的 Firestore Map 读取为 Swift 字典? - How do I read a User's Firestore Map to a Swift Dictionary? 运行 Cloud Function 将 Firebase 经过身份验证的用户数据存储到 Firestore - Run Cloud Function to store Firebase Auth'ed user data to Firestore 如果我的应用程序调用云 function 并且我根据 Firestore 用户信息验证请求,这是否与 Firestore 安全规则一样安全? - If my app calls a cloud function and I validate the request against Firestore user info, is this just as secure as a Firestore security rule?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM