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

Get the User Access Token - Flutter SDK

Cada objeto de Usuario contiene un token JWT que puedes usar para acceder a los Servicios de Atlas App.

Puede usar el token de acceso para consultar la API de Atlas GraphQL desde su aplicación cliente. Utilice cualquier cliente GraphQL para consultar la API de Atlas GraphQL, como graphql_flutterPara obtener más información sobre cómo configurar y consultar la API Atlas GraphQL, consulte API Atlas GraphQL en la documentación de App Services.

Puede obtener el token de acceso con la propiedad User.accessToken.

final token = app.currentUser?.accessToken;

El token de acceso expira 30 minutos después de que un usuario inicia sesión. No se actualiza automáticamente. Actualízalo con User.refreshCustomData().

Future<String> getValidAccessToken(User user) async {
// An already logged in user's access token might be stale. To
// guarantee that the token is valid, refresh it if necessary.
await user.refreshCustomData();
return user.accessToken;
}

También puede actualizar periódicamente el token de acceso con Timer.periodic() desde el dart:async biblioteca. Envuelva la llamada a User.refreshCustomData() con la función de devolución de llamada del temporizador.

// Refresh the token every 29 minutes
Timer.periodic(Duration(minutes: 29), (_) {
app.currentUser?.refreshCustomData();
});

Refresh tokens expire after a set period of time. When the refresh token expires, the access token can no longer be refreshed and the user must log in again.

If the refresh token expires after the realm is open, the device will not be able to sync until the user logs in again. Your sync error handler should implement logic that catches a token expired error when attempting to sync, then redirect users to a login flow.

For information on configuring refresh token expiration, refer to Manage User Sessions in the App Services documentation.

Volver

Borrar un usuario

En esta página