Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions lib/core/concurrency-queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,18 +130,23 @@ export function ConcurrencyQueue ({ axios, config }) {
//Refresh Access Token
const refreshAccessToken = async () => {
try {
// Try to refresh the token
await new OAuthHandler(axios).refreshAccessToken();
this.paused = false; // Resume the request queue once the token is refreshed

// Retry the requests that were pending due to token expiration
this.running.forEach(({ request, resolve, reject }) => {
resolve(request); // Retry the queued requests
// Retry the request
axios(request).then(resolve).catch(reject);
});
this.running = []; // Clear the running queue
this.running = []; // Clear the running queue after retrying requests
} catch (error) {
this.paused = false; // Ensure we stop queueing requests on failure
this.paused = false; // stop queueing requests on failure
this.running.forEach(({ reject }) => reject(error)); // Reject all queued requests
this.running = []; // Clear the running queue
}
};
}


const delay = (time, isRefreshToken = false) => {
if (!this.paused) {
Expand Down
3 changes: 3 additions & 0 deletions lib/core/oauthHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ export default class OAuthHandler {
*/
async authorize() {
try {
if (!this.OAuthBaseURL) {
throw new Error('OAuthBaseURL is not set');
}
const baseUrl = `${this.OAuthBaseURL}/#!/apps/${this.appId}/authorize`;
const authUrl = new URL(baseUrl);
authUrl.searchParams.set('response_type', 'code'); // Using set() to avoid duplicate parameters
Expand Down
64 changes: 59 additions & 5 deletions types/oauthHandler.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ interface OAuthResponse {
expires_in: number;
organization_uid: string;
user_uid: string;
token_type: string,
location: string,
region: string,
authorization_type: string,
stack_api_key: string
token_type: string;
location: string;
region: string;
authorization_type: string;
stack_api_key: string;
}

export default class OAuthHandler {
Expand Down Expand Up @@ -45,6 +45,60 @@ export default class OAuthHandler {
*/
getAccessToken(): string;

/**
* Get the current refresh token
* @returns The refresh token
*/
getRefreshToken(): string;

/**
* Get the current organization UID
* @returns The organization UID
*/
getOrganizationUID(): string;

/**
* Get the current user UID
* @returns The user UID
*/
getUserUID(): string;

/**
* Get the token expiry time
* @returns The token expiry time
*/
getTokenExpiryTime(): string;

/**
* Set the access token
* @param token - The access token
*/
setAccessToken(token: string): void;

/**
* Set the refresh token
* @param token - The refresh token
*/
setRefreshToken(token: string): void;

/**
* Set organization UID
* @param organizationUID - The organization UID
*/
setOrganizationUID(organizationUID: string): void;

/**
* Set user UID
* @param userUID - The user UID
*/
setUserUID(userUID: string): void;

/**
* Set expiry time
* @param expiryTime - The expiry time
*/
setTokenExpiryTime(expiryTime: Date): void;

/**
* Handle the OAuth redirect URL and exchange the authorization code for a token
* @param url - The redirect URL containing the authorization code
Expand Down