feat(oauth): add OAuth session caching to prevent multiple authentication attempts

This commit is contained in:
2025-07-22 22:56:50 +00:00
parent 1ffe02df16
commit 76c6b95f3d
6 changed files with 263 additions and 11 deletions

View File

@@ -449,6 +449,35 @@ const accounts = await bunq.getAccounts();
// According to bunq documentation:
// "Just use the OAuth Token (access_token) as a normal bunq API key"
// OAuth Session Caching (v3.0.9+)
// The library automatically caches OAuth sessions to prevent multiple authentication attempts
// Multiple instances with the same OAuth token will reuse the cached session
const bunq1 = new BunqAccount({
apiKey: 'your-oauth-access-token',
deviceName: 'OAuth App Instance 1',
environment: 'PRODUCTION',
isOAuthToken: true
});
const bunq2 = new BunqAccount({
apiKey: 'your-oauth-access-token', // Same token
deviceName: 'OAuth App Instance 2',
environment: 'PRODUCTION',
isOAuthToken: true
});
await bunq1.init(); // Creates new session
await bunq2.init(); // Reuses cached session from bunq1
// This prevents "Superfluous authentication" errors when multiple instances
// try to authenticate with the same OAuth token
// Cache management methods
BunqAccount.clearOAuthCache(); // Clear all cached OAuth sessions
BunqAccount.clearOAuthCacheForToken('token', 'PRODUCTION'); // Clear specific token
const cacheSize = BunqAccount.getOAuthCacheSize(); // Get current cache size
```
### Error Handling