Skip to content

Commit

Permalink
[SDK-3620] Release lock on pagehide (auth0#974)
Browse files Browse the repository at this point in the history
* Release lock on pagehide

* Add tests

* Remove unused field

* Add underscore prefix for private method
  • Loading branch information
frederikprijck authored Sep 8, 2022
1 parent b07721c commit 842e132
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 4 deletions.
46 changes: 45 additions & 1 deletion __tests__/Auth0Client/getTokenSilently.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ describe('Auth0Client', () => {

mockWindow.open = jest.fn();
mockWindow.addEventListener = jest.fn();
mockWindow.removeEventListener = jest.fn();

mockWindow.crypto = {
subtle: {
Expand Down Expand Up @@ -1212,6 +1213,48 @@ describe('Auth0Client', () => {
expect(releaseLockSpy).toHaveBeenCalledWith(GET_TOKEN_SILENTLY_LOCK_KEY);
});

it('should add and remove a pagehide handler', async () => {
const auth0 = setup();

jest.spyOn(<any>utils, 'runIframe').mockResolvedValue({
access_token: TEST_ACCESS_TOKEN,
state: TEST_STATE
});

await getTokenSilently(auth0);

expect(mockWindow.addEventListener).toHaveBeenCalledWith(
'pagehide',
expect.anything()
);
expect(mockWindow.removeEventListener).toHaveBeenCalledWith(
'pagehide',
expect.anything()
);
});

it('should release the lock when pagehide handler triggered', async () => {
const auth0 = setup();

jest.spyOn(<any>utils, 'runIframe').mockResolvedValue({
access_token: TEST_ACCESS_TOKEN,
state: TEST_STATE
});

mockWindow.addEventListener.mockImplementation((event, handler) => {
if (event === 'pagehide') {
handler();
expect(releaseLockSpy).toHaveBeenCalledWith(
GET_TOKEN_SILENTLY_LOCK_KEY
);
}
});

expect.assertions(1);

await getTokenSilently(auth0);
});

it('should retry acquiring a lock', async () => {
const auth0 = setup();

Expand Down Expand Up @@ -1522,7 +1565,8 @@ describe('Auth0Client', () => {
await getTokenSilently(auth0);

expect(esCookie.remove).toHaveBeenCalledWith(
`auth0.${TEST_CLIENT_ID}.organization_hint`, {}
`auth0.${TEST_CLIENT_ID}.organization_hint`,
{}
);
});

Expand Down
27 changes: 24 additions & 3 deletions src/Auth0Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,9 @@ export default class Auth0Client {
cookieDomain: this.options.cookieDomain
});
} else {
this.cookieStorage.remove(this.orgHintCookieName, { cookieDomain: this.options.cookieDomain });
this.cookieStorage.remove(this.orgHintCookieName, {
cookieDomain: this.options.cookieDomain
});
}
}

Expand Down Expand Up @@ -892,6 +894,8 @@ export default class Auth0Client {
)
) {
try {
window.addEventListener('pagehide', this._releaseLockOnPageHide);

// Check the cache a second time, because it may have been populated
// by a previous call while this call was waiting to acquire the lock.
if (!ignoreCache) {
Expand Down Expand Up @@ -936,6 +940,7 @@ export default class Auth0Client {
return authResult.access_token;
} finally {
await lock.releaseLock(GET_TOKEN_SILENTLY_LOCK_KEY);
window.removeEventListener('pagehide', this._releaseLockOnPageHide);
}
} else {
throw new TimeoutError();
Expand Down Expand Up @@ -1048,8 +1053,12 @@ export default class Auth0Client {
}

const postCacheClear = () => {
this.cookieStorage.remove(this.orgHintCookieName, { cookieDomain: this.options.cookieDomain });
this.cookieStorage.remove(this.isAuthenticatedCookieName, { cookieDomain: this.options.cookieDomain });
this.cookieStorage.remove(this.orgHintCookieName, {
cookieDomain: this.options.cookieDomain
});
this.cookieStorage.remove(this.isAuthenticatedCookieName, {
cookieDomain: this.options.cookieDomain
});

if (localOnly) {
return;
Expand Down Expand Up @@ -1308,4 +1317,16 @@ export default class Auth0Client {
return entry.access_token;
}
}

/**
* Releases any lock acquired by the current page that's not released yet
*
* Get's called on the `pagehide` event.
* https://developer.mozilla.org/en-US/docs/Web/API/Window/pagehide_event
*/
private _releaseLockOnPageHide = async () => {
await lock.releaseLock(GET_TOKEN_SILENTLY_LOCK_KEY);

window.removeEventListener('pagehide', this._releaseLockOnPageHide);
};
}

0 comments on commit 842e132

Please sign in to comment.