Skip to content

Commit

Permalink
fix: handleLoginRedirect should not subscribe to auth state changes
Browse files Browse the repository at this point in the history
OKTA-391495
<<<Jenkins Check-In of Tested SHA: 2e329e8 for eng_productivity_ci_bot_okta@okta.com>>>
Artifact: okta-auth-js
Files changed count: 6
PR Link: "#730"
  • Loading branch information
aarongranick-okta authored and eng-prod-CI-bot-okta committed May 13, 2021
1 parent 60e13da commit f8f7c7c
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 208 deletions.
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
{
"type": "node",
"request": "launch",
"name": "jest (jsdom)",
"name": "jest (browser, jsdom)",
"program": "${workspaceFolder}/node_modules/jest/bin/jest",
"args": [
"--runInBand",
Expand All @@ -21,7 +21,7 @@
{
"type": "node",
"request": "launch",
"name": "jest (node)",
"name": "jest (server, node)",
"program": "${workspaceFolder}/node_modules/jest/bin/jest",
"args": [
"--runInBand",
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 5.1.0

### Features

- [#730](https://github.com/okta/okta-auth-js/pull/730) `updateAuthState` returns a Promise.

## 5.0.0

### Features
Expand Down
24 changes: 19 additions & 5 deletions lib/AuthStateManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export class AuthStateManager {
return this._authState;
}

updateAuthState(): void {
async updateAuthState(): Promise<AuthState> {
const { transformAuthState, devMode } = this._sdk.options;

const log = (status) => {
Expand All @@ -92,12 +92,22 @@ export class AuthStateManager {
devMode && log('emitted');
};

const finalPromise = (origPromise) => {
return this._pending.updateAuthStatePromise.then(() => {
const curPromise = this._pending.updateAuthStatePromise;
if (curPromise && curPromise !== origPromise) {
return finalPromise(curPromise);
}
return this.getAuthState();
});
};

if (this._pending.updateAuthStatePromise) {
if (this._pending.canceledTimes >= MAX_PROMISE_CANCEL_TIMES) {
// stop canceling then starting a new promise
// let existing promise finish to prevent running into loops
devMode && log('terminated');
return;
return finalPromise(this._pending.updateAuthStatePromise);
} else {
this._pending.updateAuthStatePromise.cancel();
}
Expand All @@ -117,10 +127,12 @@ export class AuthStateManager {
resolve();
return;
}
// emit event and clear pending states
emitAuthStateChange(authState);
this._pending = { ...DEFAULT_PENDING };
// emit event and resolve promise
emitAuthStateChange(authState);
resolve();

// clear pending states after resolve
this._pending = { ...DEFAULT_PENDING };
};

this._sdk.isAuthenticated()
Expand Down Expand Up @@ -154,6 +166,8 @@ export class AuthStateManager {
});
/* eslint-enable complexity */
this._pending.updateAuthStatePromise = cancelablePromise;

return finalPromise(cancelablePromise);
}

subscribe(handler): void {
Expand Down
39 changes: 17 additions & 22 deletions lib/OktaAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import {
PkceAPI,
SigninOptions,
IdxAPI,
SignoutRedirectUrlOptions
SignoutRedirectUrlOptions,
} from './types';
import {
transactionStatus,
Expand Down Expand Up @@ -554,33 +554,28 @@ class OktaAuth implements SigninAPI, SignoutAPI {
}

async handleLoginRedirect(tokens?: Tokens): Promise<void> {
const handleRedirect = async () => {
// Unsubscribe listener
this.authStateManager.unsubscribe(handleRedirect);

// Get and clear originalUri from storage
const originalUri = this.getOriginalUri();
this.removeOriginalUri();

// Redirect to originalUri
const { restoreOriginalUri } = this.options;
if (restoreOriginalUri) {
await restoreOriginalUri(this, originalUri);
} else {
window.location.replace(originalUri);
}
};

// Handle redirect after authState is updated
this.authStateManager.subscribe(handleRedirect);

// Store tokens and update AuthState by the emitted events
if (tokens) {
this.tokenManager.setTokens(tokens);
} else if (this.isLoginRedirect()) {
await this.storeTokensFromRedirect();
} else {
this.authStateManager.unsubscribe(handleRedirect);
return; // nothing to do
}

// ensure auth state has been updated
await this.authStateManager.updateAuthState();

// Get and clear originalUri from storage
const originalUri = this.getOriginalUri();
this.removeOriginalUri();

// Redirect to originalUri
const { restoreOriginalUri } = this.options;
if (restoreOriginalUri) {
await restoreOriginalUri(this, originalUri);
} else {
window.location.replace(originalUri);
}
}

Expand Down
Loading

0 comments on commit f8f7c7c

Please sign in to comment.