Overview
Puede utilizar el SDK web para crear y administrar Claves API de usuario que permiten que los servicios inicien sesión e interactúen con su aplicación en nombre de un usuario existente sin necesidad de que el usuario vuelva a autenticarse.
Las claves API de usuario se administran como parte del proveedor de autenticación de claves API, pero solo se pueden crear para usuarios asociados con un proveedor de autenticación diferente y no anónimo.
Los objetos de usuario en el SDK web incluyen un ApiKeyAuth objeto que expone métodos para trabajar con las claves API de ese usuario.
Create a User API Key
Para crear una nueva clave API de usuario, llama a ApiKeyAuth.create() con un nombre identificativo para la clave. El nombre debe ser una string única entre todas las claves API del usuario.
Advertencia
Store the API Key Value
The SDK only 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.
const user = app.currentUser; const key = await user.apiKeys.create("myApiKey");
Look up a User API Key
Para obtener un arreglo que enumere todas las claves de API de un usuario, llama a ApiKeyAuth.fetchAll(). También puedes encontrar una clave de API específica llamando a ApiKeyAuth.fetch() con el _id de la clave.
const user = app.currentUser; // 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(API_KEY_ID);
Habilitar o deshabilitar una clave de API
Puede habilitar o deshabilitar una clave API de usuario llamando a ApiKeyAuth.enable() o ApiKeyAuth.disable() con el _id de la clave. Cuando una clave está deshabilitada, no se puede usar para iniciar sesión en nombre del usuario.
// Get the ID of a User API Key const user = app.currentUser; const apiKeys = await user.apiKeys.fetchAll(); const keyId = apiKeys[0]["_id"]; // Enable the User API Key await user.apiKeys.enable(keyId); // Disable the User API Key await user.apiKeys.disable(keyId);
Eliminar una clave API
You can permanently delete a user API key by calling ApiKeyAuth.delete() with the key's _id. Deleted keys can no longer be used to log in on behalf of the user.
// Get the ID of a User API Key const user = app.currentUser; const apiKeys = await user.apiKeys.fetchAll(); const keyId = apiKeys.find((key) => key.name === "apiKeyToDelete")._id; // Delete the User API Key await user.apiKeys.delete(keyId);