Join us at MongoDB.local London on 7 May to unlock new possibilities for your data. Use WEB50 to save 50%.
Register now >
Docs Menu
Docs Home
/ /
Administrar usuarios

Datos de usuario personalizados - SDK React Native

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.

Antes de poder trabajar con datos de usuario personalizados desde tu aplicación React Native, debes habilitarlos en tu App Services App. Para obtener más información, consulta Enable Custom User Data.

Advertencia

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.

Si no ha actualizado recientemente sus datos de usuario personalizados, utilice el campo customData del objeto de usuario. customData El campo es de sólo lectura.

Si ha actualizado sus datos de usuario personalizados en los últimos 30 minutos, utilice 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);
}
// ...
}

Nota

Para modificar el campo de datos personalizados desde una función de cliente o usuario, debe configurarse el permiso de escritura en la colección donde se almacenan los datos personalizados. Si prefieres restringir el acceso de escritura de los clientes a los datos personalizados de tu aplicación, aún puedes modificar el objeto desde una función del sistema.

Volver

Authenticate Users

En esta página