Skip to content

Commit

Permalink
Callback executed too late
Browse files Browse the repository at this point in the history
  • Loading branch information
rolljee committed Dec 13, 2024
1 parent 5547114 commit fd18532
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/commands/sdk/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ Default fallback to API action

static readStdin = true;

async runSafe() {
async runSafe(): Promise<void> {
const [controller, action] = this.args["controller:action"].split(":");

const requestArgs: any = {};
Expand Down
13 changes: 10 additions & 3 deletions src/support/kuzzle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ export class KuzzleSDK {
public async impersonate(userKuid: string, callback: { (): Promise<void> }) {
const currentToken = this.sdk.jwt;
let apiKey: any;
let callbackResult;

try {
console.log('Starting impersonation process...');
Expand All @@ -168,15 +169,17 @@ export class KuzzleSDK {
this.sdk.jwt = apiKey._source.token;

console.log('Executing callback...');
await callback();
callbackResult = await callback(); // Store the result
console.log('Callback completed successfully');

return callbackResult; // Return the callback result
} catch (error) {
console.error('Error during impersonation:', error);
throw error;
} finally {
console.log("Starting cleanup in finally block...");
this.sdk.jwt = currentToken;

// Move token restoration after cleanup
if (apiKey?._id) {
try {
await this.security.deleteApiKey(userKuid, apiKey._id);
Expand All @@ -185,11 +188,15 @@ export class KuzzleSDK {
console.error('Error cleaning up API key:', cleanupError);
}
}
console.log("Cleanup completed");

// Restore token last
this.sdk.jwt = currentToken;
console.log("Token restored and cleanup completed");
}
}



disconnect() {
this.sdk.disconnect();

Expand Down

0 comments on commit fd18532

Please sign in to comment.