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

Create & Manage User API Keys - React Native SDK

User API keys allow devices or services to communicate with App Services on behalf of a user without sharing that users credentials. User API keys can be revoked at any time by the authenticated user. User API keys do not expire on their own.

Puede administrar una clave API de usuario con la ClienteApiKeyAuth al que se accede con la propiedad User.apiKeys de un usuario autenticado.

Si está usando @realm/react, puede acceder al cliente ApiKeyAuth de un usuario con el gancho useUser() en un componente envuelto por UserProvider.

import {useUser} from '@realm/react';
const user = useUser();
const createUserApiKey = async () => {
const {_id, key, name, disabled} = await user?.apiKeys.create(apiKeyName);
// ...Do something with API key like save it
// or share it with external service that authenticates
// on user's behalf.
};

Para crear una nueva clave API de usuario, pase un nombre que sea único entre todas las claves API del usuario a ApiKeyAuth.create().

The SDK returns the value of the user API key when you create it. Make sure to store the key value securely so that you can use it to log in. If you lose or do not store the key value there is no way to recover it. You will need to create a new user API key.

You cannot create a user API key for a server API key or an anonymous user.

const {_id, key, name, disabled} = await user?.apiKeys.create(apiKeyName);

Para obtener un arreglo que liste todas las claves API de un usuario, llame a ApiKeyAuth.fetchAll().

To find a specific API key, pass the key's _id to ApiKeyAuth.fetch().

const getUserApiKey = async () => {
// List all of a user's keys
const keys = await user.apiKeys.fetchAll();
// Get a specific key by its ID
const key = await user!.apiKeys.fetch(apiKey!._id);
};

Para habilitar o deshabilitar una clave API de usuario, pasa el _id de la clave a ApiKeyAuth.enable() o ApiKeyAuth.disable(). Cuando una clave está deshabilitada, no puede usarse para iniciar sesión en nombre del usuario.

await user!.apiKeys.enable(cloudApiKey!._id);
await user!.apiKeys.disable(cloudApiKey!._id);

To permanently delete a user API, pass the key's _id to ApiKeyAuth.delete(). Deleted keys cannot be recovered.

await user!.apiKeys.delete(cloudApiKey!._id);

Volver

Vincular identidades de usuario

En esta página