Skip to content

Commit

Permalink
Resolve signIn promise when sign in has completed (#227)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnyd710 authored Dec 14, 2023
1 parent cd4c148 commit 833e9d6
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "minor",
"comment": "Resolve ElectronRendererAuthorization signIn when sign in has completed and reject if an error occurs.",
"packageName": "@itwin/electron-authorization",
"email": "[email protected]",
"dependentChangeType": "patch"
}
61 changes: 34 additions & 27 deletions packages/electron/src/main/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -398,40 +398,47 @@ export class ElectronMainAuthorization implements AuthorizationClient {
// Setup a notifier to obtain the result of authorization
const notifier = new AuthorizationNotifier();
authorizationHandler.setAuthorizationNotifier(notifier);
notifier.setAuthorizationListener(
async (
authRequest: AuthorizationRequest,
authResponse: AuthorizationResponse | null,
authError: AuthorizationError | null
) => {
Logger.logTrace(
loggerCategory,
"Authorization listener invoked",
() => ({ authRequest, authResponse, authError })
);

const tokenResponse = await this._onAuthorizationResponse(
authRequest,
authResponse,
authError
);
authorizationEvents.onAuthorizationResponseCompleted.raiseEvent(
authError ? authError : undefined
);

if (tokenResponse)
// await this.saveRefreshToken(tokenResponse);
await this.processTokenResponse(tokenResponse);
else
await this.clearTokenCache();
}
);
const tokenRequestCompleted = new Promise<void>((finished, reject) => {
notifier.setAuthorizationListener(
async (
authRequest: AuthorizationRequest,
authResponse: AuthorizationResponse | null,
authError: AuthorizationError | null
) => {
Logger.logTrace(
loggerCategory,
"Authorization listener invoked",
() => ({ authRequest, authResponse, authError })
);

const tokenResponse = await this._onAuthorizationResponse(
authRequest,
authResponse,
authError
).catch((e) => reject(e));

authorizationEvents.onAuthorizationResponseCompleted.raiseEvent(
authError ? authError : undefined
);

if (tokenResponse)
await this.processTokenResponse(tokenResponse);
else
await this.clearTokenCache();

finished();
}
);
});

// Start the signin
await authorizationHandler.performAuthorizationRequest(
this._configuration,
authorizationRequest
);

return tokenRequestCompleted;
}

/**
Expand Down
6 changes: 5 additions & 1 deletion packages/electron/src/renderer/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,11 @@ export class ElectronRendererAuthorization implements AuthorizationClient {
});
}

/** Called to start the sign-in process. Subscribe to onAccessTokenChanged to be notified when sign-in completes */
/**
* Called to start the sign-in process.
* Resolves when sign-in completes, rejects if sign-in fails.
* Subscribe to `onAccessTokenChanged` to be notified when a token is available.
*/
public async signIn(): Promise<void> {
await this._ipcAuthAPI.signIn();
}
Expand Down

0 comments on commit 833e9d6

Please sign in to comment.