auth0-cc-token-cache
v0.1.0
Published
Auth0 client credentials token acquisition with Redis caching
Maintainers
Readme
auth0-cc-token-cache
Minimal Auth0 client credentials token acquisition with Redis caching.
Solves the common problem of M2M token quota limits by caching tokens in Redis with automatic TTL management.
Install
npm install auth0-cc-token-cache auth0 ioredisUsage
import { createTokenClient } from 'auth0-cc-token-cache';
import Redis from 'ioredis';
const redis = new Redis(process.env.REDIS_URL);
const tokenClient = createTokenClient({
auth0: {
domain: 'your-tenant.auth0.com',
clientId: process.env.AUTH0_CLIENT_ID,
clientSecret: process.env.AUTH0_CLIENT_SECRET,
},
audience: 'https://your-api.example.com',
cache: {
redis,
keyPrefix: 'my-app', // optional, default: 'auth0-cc'
ttlBuffer: 60, // optional, seconds before expiry to refresh, default: 60
},
});
const token = await tokenClient.getAccessToken();How it works
- Checks Redis for a cached token
- If found, returns it immediately
- If not found, calls Auth0's client credentials grant via the official SDK
- Caches the token in Redis with TTL =
expires_in - ttlBuffer - Returns the token
The ttlBuffer ensures tokens are refreshed before they actually expire, avoiding edge cases where a token expires mid-request.
Cache key format: {keyPrefix}:{clientId}:{audience}
API
createTokenClient(config): Auth0CCTokenClient
Config
interface TokenClientConfig {
auth0: {
domain: string; // Your Auth0 domain (e.g., 'tenant.auth0.com')
clientId: string; // Application client ID
clientSecret: string; // Application client secret
};
audience: string; // API identifier
cache: {
redis: Redis; // ioredis instance
keyPrefix?: string; // Cache key prefix (default: 'auth0-cc')
ttlBuffer?: number; // Seconds before expiry to refresh (default: 60)
};
}Returns
interface Auth0CCTokenClient {
getAccessToken(): Promise<string>;
}Requirements
- Node.js >= 18
- Redis instance
- Auth0 application with client credentials grant enabled
License
MIT
