Multi-Resource Refresh Tokens (MRRT) allow your application to obtain access tokens for multiple APIs (each with a different audience) using a single refresh token. This eliminates the need to perform a new login when switching between different backend services.
Prerequisites
- Enable MRRT on your Auth0 tenant (via Dashboard or Support).
- Request
offline_access scope during the initial login to ensure a refresh token is issued. - Register all target APIs in the Auth0 Dashboard with their respective audience identifiers.
Implementation with Hooks
Use getApiCredentials to fetch tokens for specific audiences and clearApiCredentials to manage the cache.
import { useAuth0 } from 'react-native-auth0';
function MyComponent() {
const { authorize, getApiCredentials, clearApiCredentials } = useAuth0();
const login = async () => {
// 1. Login with offline_access to get the refresh token
await authorize({
scope: 'openid profile email offline_access',
audience: 'https://primary-api.example.com',
});
};
const getFirstApiToken = async () => {
try {
// 2. Get credentials for a specific API
const credentials = await getApiCredentials(
'https://first-api.example.com',
'read:data write:data'
);
console.log('First API authenticated successfully');
} catch (error) {
console.error('Error:', error);
}
};
const clearFirstApiCache = async () => {
// 3. Clear cached credentials for a specific API or scope
await clearApiCredentials('https://first-api.example.com');
// Or specific scope:
await clearApiCredentials('https://first-api.example.com', 'read:data');
};
return <></>;
}
import { useAuth0 } from 'react-native-auth0';
function MyComponent() {
const { authorize, getApiCredentials, clearApiCredentials } = useAuth0();
const login = async () => {
// Login with offline_access to get a refresh token
await authorize({
scope: 'openid profile email offline_access',
audience: 'https://primary-api.example.com',
});
};
const getFirstApiToken = async () => {
try {
// Get credentials for the first API
const credentials = await getApiCredentials(
'https://first-api.example.com',
'read:data write:data'
);
console.log('First API authenticated successfully');
console.log('Expires At:', new Date(credentials.expiresAt * 1000));
} catch (error) {
console.error('Error:', error);
}
};
const getSecondApiToken = async () => {
try {
// Get credentials for a different API using the same refresh token
const credentials = await getApiCredentials(
'https://second-api.example.com',
'read:reports'
);
console.log('Second API authenticated successfully');
} catch (error) {
console.error('Error:', error);
}
};
const clearFirstApiCache = async () => {
// Clear cached credentials for a specific API
await clearApiCredentials('https://first-api.example.com');
// Or clear with specific scope
await clearApiCredentials('https://first-api.example.com', 'read:data');
};
return (
// Your UI components
);
}