Navigation

Configure Custom User Data

On this page

Overview

You can store arbitrary data about your application users in a MongoDB collection and configure Stitch to automatically expose each user’s data in a field of their user object. For example, you might store a user’s preferred language, date of birth, or their local timezone.

Stitch automatically finds a user’s custom data document and includes it in their access token when they log in. You can access the data in the custom_data field of the user’s object in function context, the %%user expansion, and their client application access token.

Stitch does not manage custom user documents so you are responsible for creating and deleting them. The underlying data is a regular MongoDB document, so you can use standard CRUD operations through the MongoDB Atlas service to define and modify a user’s custom data. You can also use authentication triggers to dynamically update user documents, such as storing the time of their most recent login in the lastLogin field.

Store One Document Per User

Documents that contain user data must contain the user’s ID in a specific field. If multiple documents specify the same user’s ID, Stitch only exposes the data from the document that was inserted first.

Custom Data May Be Stale

Stitch does not dynamically update a user’s custom data if the underlying document changes. Instead, Stitch fetches a new copy of the data whenever a user refreshes their access token, such as when they log in. This may mean that the custom data won’t immediately reflect changes, e.g. updates from an authentication trigger. The client SDKs automatically refresh a user’s access token periodically when they’re logged in, so the user’s custom data should not be stale for more than 30 minutes.

Procedure

1
2

Enable Custom User Data

To configure Stitch to associate data in a collection with your application’s users, set the Enable Custom User Data toggle to On.

3

Specify the Custom User Data Collection

You must store the custom data for your application’s users in a single collection of a linked MongoDB Atlas cluster. To configure your application to read user data from this collection, you need to specify the following values:

  • Cluster Name: The name of a linked MongoDB cluster that contains the custom user data collection.
  • Database Name: The name of the MongoDB database that contains the custom user data collection.
  • Collection Name: The name of the MongoDB collection that contains custom user data.
4

Specify the User ID Field

Every document in the custom user data collection should have a field that contains the user ID of the Stitch user that it describes. Specify the name of the field that contains each user’s ID in the User ID Field input.

5

Deploy the Updated Application

Once you have configured the customer user data collection, you can make custom user data available to client applications by deploying your application. To deploy a draft application from the Stitch UI:

  1. Click Deploy in the left-hand navigation
  2. Find the draft in the deployment history table and then click Review & Deploy Changes.
  3. Review the diff of changes and then click Deploy.

Once the application successfully deploys, Stitch begins to associate custom data with users. When a user logs in, Stitch automatically queries the custom user data collection for a document where the specified User ID Field contains the user’s ID. If a document matches, Stitch exposes the data in the document in the custom_data field of that user’s user object.

Example

You can access custom user data from a StitchUser object in functions and rule expressions.

const user = context.user;
const likesBlue = user.custom_data.favoriteColor === "blue";
{
  "%%user.custom_data.favoriteColor": "blue"
}
1

Export Your Stitch Application

You can set up and enable custom user data through Stitch CLI or automatic GitHub deployment. To configure custom user data, you need the latest version of the application directory for your application.

You can export your application from the Export tab of the Settings page in the Stitch UI, or by running the following command from an authenticated instance of stitch-cli:

stitch-cli export --app-id=<App ID>
2

Configure Custom User Data

You must store the custom data for your application’s users in a single collection of a linked MongoDB Atlas cluster. Every document in the collection should have a field that contains the Stitch user ID of the user it describes.

To configure your application to read user data from this collection, specify a configuration document in the custom_user_data_config field of stitch.json:

stitch.json
{
  ...,
  "custom_user_data_config": {
    "enabled": <boolean>,
    "mongo_service_id": "<Linked MongoDB Cluster Service ID>",
    "database_name": "<Database Name>",
    "collection_name": "<Collection Name>",
    "user_id_field": "<User ID Field Name>"
  }
}
Field Description
enabled A boolean that, if true, indicates that Stitch should associate data in the specified collection with application users.
mongo_service_id The service ID of the linked MongoDB cluster that contains the custom user data collection.
database_name The name of the MongoDB database that contains the custom user data collection.
collection_name The name of the MongoDB collection that contains custom user data.
user_id_field The name of a field that contains an application user’s ID value. All user documents in the collection must have this field and no two documents may contain the same user ID.
3

Deploy the Updated Application

Once you have configured the customer user data collection, you can make custom user data available to client applications by deploying your application.

To deploy a draft application with Stitch CLI:

stitch-cli import

To deploy a draft application with automatic GitHub deployment:

git add stitch.json
git commit -m "Configure and Enable Custom User Data"
git push origin master

Once the application successfully deploys, Stitch begins to associate custom data with users. When a user logs in, Stitch automatically queries the custom user data collection for a document where the specified User ID Field contains the user’s ID. If a document matches, Stitch exposes the data in the document in the custom_data field of that user’s user object.

Example

You can access custom user data from a StitchUser object in functions and rule expressions.

const user = context.user;
const likesBlue = user.custom_data.favoriteColor === "blue";
{
  "%%user.custom_data.favoriteColor": "blue"
}