Docs 菜单

Docs 主页开发应用程序Atlas Device SDKs

自定义用户数据 - C++ SDK

在此页面上

  • 读取用户的自定义数据
  • 创建用户的自定义数据文档
  • 更新用户的自定义数据
  • 删除用户的自定义数据

您可以通过当前登录用户的 User对象读取该用户的自定义用户数据。您无法通过User对象编辑自定义用户数据。要编辑自定义用户数据,请参阅更新自定义用户数据。要读取数据,请访问已登录用户的User对象上的custom_data属性:

// Custom user data could be stale, so refresh it before reading it
user.refresh_custom_user_data().get();
auto userData = user.custom_data().value();
/* Parse the string custom data to use it more easily in your code.
In this example, we're using the nlohmann/json library, but use whatever
works with your application's constraints. */
auto userDataObject = nlohmann::json::parse(userData);
CHECK(userDataObject["favoriteColor"] == "gold");

警告

自定义数据可能已过时

当基础数据发生变化时,Atlas App Services 不会立即动态更新客户端用户自定义数据文档的值。相反,每当用户刷新其访问令牌时,App Services 都会获取最新版本的自定义用户数据,而大多数联系 App Services 后端的 SDK 操作都会使用该令牌。如果令牌在其默认30分钟到期时间之前未刷新,则 C++ SDK 会在下次调用后端时刷新令牌。自定义用户数据可能会过时长达30分钟,加上 SDK 下次调用后端之前的时间。

注意

如果您需要最新版本的自定义用户数据,请使用refresh_custom_user_data()函数请求最新版本的用户自定义数据。

要为用户创建自定义用户数据,请在自定义用户数据集合中创建 MongoDB 文档。 文档的用户 ID 字段应包含用户的用户 ID。

提示

在 App Services 用户界面中,检查Custom User Data标签页下的 App Users页面,查找并配置自定义用户数据设置,包括:

  • 自定义用户数据集群、数据库和collection

  • 用于将自定义用户数据文档映射到用户的用户 ID 字段

创建此文档的一种方法是调用Atlas Function ,该函数将自定义数据文档插入到自定义用户数据集合中。从 Atlas Function 添加自定义用户数据没有单一的模式。您应该编写一个或多个函数以适合应用程序的使用案例。

在此示例中,Realm 函数获取客户端传递的对象,并将其添加到 Atlas 的自定义用户数据collection中。如果自定义用户数据尚不存在,则该函数将创建该数据;如果该数据存在,则该函数将替换其中的所有数据。

updateCustomUserData.js - 在服务器上运行的 Atlas Function (JavaScript)
exports = async function updateCustomUserData(newCustomUserData) {
const userId = context.user.id;
const customUserDataCollection = context.services
.get("mongodb-atlas")
.db("custom-user-data-database")
.collection("cpp-custom-user-data");
const filter = { userId };
// Replace the existing custom user data document with the new one.
const update = { $set: newCustomUserData };
// Insert document if it doesn't already exist
const options = { upsert: true };
const res = await customUserDataCollection.updateOne(filter, update, options);
return res;
};

以下示例调用函数以将包含当前登录用户的用户ID和favoriteColor值的文档插入到自定义用户数据集合中:

auto user = app.login(realm::App::credentials::anonymous()).get();
// Functions take a string argument. Any quotes within the array must be
// escaped.
auto customData =
"[{\"userId\":\"" + user.identifier() + "\",\"favoriteColor\":\"gold\"}]";
// Call an Atlas Function to insert custom data for the user
auto result = user.call_function("updateCustomUserData", customData).get();

在创建自定义用户数据文档时,您可以向其中添加任意数量的字段和值。用户 ID 字段是文档要在User对象上作为自定义用户数据使用的唯一要求。

您可以使用Atlas FunctionMongoDB CompassMongoDB Atlas Data Explorer 更新自定义用户数据。

要使用 Realm 函数 更新用户的自定义用户数据,请编辑用户ID 字段包含该用户的用户ID 的 MongoDB 文档。以下示例调用上面用于创建自定义用户数据文档的同一函数。 在这里,我们更新包含当前登录用户的用户 ID 的文档的favoriteColor字段:

// Functions take a string argument. Any quotes within the array must be
// escaped.
auto updatedData = "[{\"userId\":\"" + user.identifier() +
"\",\"favoriteColor\":\"black\"}]";
// Call an Atlas Function to update custom data for the user
auto updateResult =
user.call_function("updateCustomUserData", updatedData).get();
// Refresh the custom user data before reading it to verify it succeeded
user.refresh_custom_user_data().get();
auto updatedUserData = user.custom_data().value();
/* Parse the string custom data to use it more easily in your code.
In this example, we're using the nlohmann/json library, but use whatever
works with your application's constraints. */
auto updatedUserDataObject = nlohmann::json::parse(updatedUserData);
CHECK(updatedUserDataObject["favoriteColor"] == "black");

提示

要确定用户的 ID,请访问user.identifier()属性或在Users标签页下App Users页面上的 App Services 用户界面中查找该用户。

自定义用户数据存储在链接到用户对象的文档中。删除用户不会删除自定义用户数据。完全删除用户数据以遵守 Apple 的帐户删除指南 等 ,您必须手动删除用户的自定义数据文档。

您可以使用Atlas FunctionMongoDB CompassMongoDB Atlas Data Explorer 删除自定义用户数据。

在此示例中,Realm 函数不需要任何参数。该函数使用函数上下文来确定调用者的用户 ID,并删除与该用户 ID 匹配的自定义用户数据文档。

deleteCustomUserData.js — 在服务器上运行的 Realm 函数 (JavaScript)
exports = async function deleteCustomUserData() {
const userId = context.user.id;
const customUserDataCollection = context.services
.get("mongodb-atlas")
.db("custom-user-data-database")
.collection("cpp-custom-user-data");
const filter = { userId };
const res = await customUserDataCollection.deleteOne(filter);
return res;
};

调用此函数的代码只需登录用户即可调用该函数:

auto deleteResult = user.call_function("deleteCustomUserData", "[]").get();
← 托管电子邮件/密码用户 — C++ SDK