feat(oauth): add OAuth session caching to prevent multiple authentication attempts
This commit is contained in:
@@ -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}`);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user