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

@@ -162,4 +162,40 @@ export class BunqApiContext {
public getBaseUrl(): string {
return this.context.baseUrl;
}
/**
* Check if the context has a valid session
*/
public hasValidSession(): boolean {
return this.session && this.session.isSessionValid();
}
/**
* Initialize with existing OAuth session (skip installation/device/session creation)
*/
public async initWithExistingSession(): Promise<void> {
// For OAuth tokens that already have a session, we just need to:
// 1. Use the OAuth token as the session token
// 2. Set OAuth mode for proper expiry handling
this.context.sessionToken = this.options.apiKey;
// Create session instance with existing token
this.session = new BunqSession(this.crypto, this.context);
this.session.setOAuthMode(true);
// Try to get user info to validate the session
try {
// This will test if the session is valid
const testClient = this.session.getHttpClient();
const response = await testClient.get('/v1/user');
if (response && response.Response) {
console.log('Successfully reused existing OAuth session');
await this.saveContext();
}
} catch (error) {
throw new Error(`Failed to reuse OAuth session: ${error.message}`);
}
}
}