Docs Menu
Docs Home
/ /
Atlas Device SDKs
/ /

Custom User Data - React Native SDK

On this page

  • Prerequisites
  • Read Custom User Data
  • Write Custom User Data

You can read arbitrary data about your application users, known as custom user data, directly within your React Native application. For example, you might store a user's preferred language, date of birth, or local timezone.

Before you can work with custom user data from your React Native app, you must enable it in your App Services App. To learn more, refer to Enable Custom User Data.

Warning

Custom Data May Be Stale

App Services does not dynamically update a user's custom data if the underlying document changes. Instead, App Services 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. If the token is not refreshed, the SDK waits 30 minutes and then refreshes it on the next call to the backend, so custom user data could be stale for up to 30 minutes plus the time until the next SDK call to the backend occurs.

If you have not recently updated your custom user data, use the user object's customData field. The customData field is read only.

If you have updated your custom user data within the last 30 minutes, use User.refreshCustomData().

import React, {useState, useEffect} from 'react';
import {useApp, useUser} from '@realm/react';
function ReadCustomUserData() {
const user = useUser();
const [customUserData, setCustomUserData] = useState();
// Access current custom user data with `user.customData`
function readCurrentCustomUserData() {
setCustomUserData(user.customData);
}
// Refresh custom user data with `user.refreshCustomData()`
async function refreshCustomUserData() {
const data = await user.refreshCustomData();
setCustomUserData(data);
}
// ...
}

You can write to a user's custom user data with MongoDB Data Access.

Your write operations must include the User.id in the User ID Field you set when configuring custom user data in the App backend. If you don't include the user ID in the User ID Field, the data that you write will not be linked to the user's custom user data.

import React, {useEffect} from 'react';
import {useApp, useUser} from '@realm/react';
function WriteCustomUserData() {
const user = useUser();
async function writeCustomUserData(favoriteColor: string) {
const customUserDataCollection = user
.mongoClient('mongodb-atlas')
.db('custom-user-data-database')
.collection('custom-user-data');
const filter = {
userId: user.id, // Query for the user object of the logged in user
};
const updateDoc = {
$set: {
// Set User ID if it's not already set
userId: user.id,
// Set the logged in user's favorite color
favoriteColor,
},
};
const options = {upsert: true};
await customUserDataCollection.updateOne(filter, updateDoc, options);
// Refresh custom user data once it's been updated on the server
const customUserData = await user.refreshCustomData();
console.log(customUserData);
}
// ...
}

Note

To modify the custom data field from a client or user function, write permission to the collection in which custom data is stored must be configured. If you prefer to restrict client write access to custom data from your application, you can still modify the object from a system function.

Back

Authenticate Users