Skip to content

Commit

Permalink
Fix async issue with unviversal SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
WietseWind committed Mar 24, 2023
1 parent d50f6ce commit 35700b3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "xumm-oauth2-pkce",
"version": "2.7.7",
"version": "2.7.8",
"description": "Xumm JS SDK for client side only OAuth2 PKCE (implicit flow) auth.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand All @@ -9,7 +9,7 @@
"clean": "rm -rf dist",
"build": "tsc",
"buildsample": "mkdir -p ./sample/dist; cp ./dist/browser.min.js ./sample/dist/",
"serve": "npm run build && npm run browserify && serve -C -n -p 3001 ./",
"serve": "npm run build && npm run browserify && serve -C -n -p 3002 ./",
"watch": "tsc -w",
"inspect": "clear && npm run build && node --inspect dist/samples/sample.js",
"browserify": "browserify --standalone XummPkce -r ./dist/index.js:XummPkce |sed 's/\\(exports.XummPkce = XummPkce;\\)/\\1 module.exports = XummPkce;/g' > dist/browser.js && npm run minify",
Expand Down
15 changes: 7 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export class XummPkceThread extends EventEmitter {

private jwt?: string;

private ping?: Promise<any>;
private ping?: ReturnType<XummSdkJwt['ping']>;
private resolved = false;
private resolvedSuccessfully?: boolean;
private resolvePromise?: (result: ResolvedFlow) => void;
Expand Down Expand Up @@ -176,7 +176,7 @@ export class XummPkceThread extends EventEmitter {

if (existingJwt?.jwt && typeof existingJwt.jwt === "string") {
const sdk = new XummSdkJwt(existingJwt.jwt);
this.ping = sdk.ping();
this.ping = sdk.ping()
this.ping
.then(async (pong) => {
/**
Expand All @@ -185,8 +185,8 @@ export class XummPkceThread extends EventEmitter {
if (pong?.jwtData?.sub) {
// Yay, user still signed in, JWT still valid!
this.autoResolvedFlow = Object.assign(existingJwt, { sdk });
this.emit("retrieved");
await this.authorize();
this.emit("retrieved");
} else {
this.logout();
}
Expand Down Expand Up @@ -420,16 +420,15 @@ export class XummPkceThread extends EventEmitter {
}

public async authorize() {
if (this.options.rememberJwt) {
await Promise.race([this.ping, this.eventPromises.loggedout.promise]);
}

// Do not authorize twice
if (this.resolvedSuccessfully) {
return this.promise;
}

this.resolved = false;

await this.ping

if (!this.mobileRedirectFlow && !this.autoResolvedFlow) {
const url = this.authorizeUrl();
const popup = window.open(
Expand Down

0 comments on commit 35700b3

Please sign in to comment.