From b92547256fb275368847889b792ec7565e6b00fc Mon Sep 17 00:00:00 2001 From: Steve Hobbs Date: Fri, 24 Jan 2020 11:23:12 +0000 Subject: [PATCH] No longer acquires a browser lock if there was a hit on the cache (#339) --- __tests__/index.test.ts | 11 +++-------- src/Auth0Client.ts | 5 ++--- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/__tests__/index.test.ts b/__tests__/index.test.ts index 0de29d860..d9f2ebdf3 100644 --- a/__tests__/index.test.ts +++ b/__tests__/index.test.ts @@ -1073,18 +1073,13 @@ describe('Auth0', () => { expect(token).toBe(TEST_ACCESS_TOKEN); }); - it('acquires and releases lock when there is a cache', async () => { + it('does not acquire a lock when the cache is available', async () => { const { auth0, cache, lock } = await setup(); cache.get.mockReturnValue({ access_token: TEST_ACCESS_TOKEN }); await auth0.getTokenSilently(); - expect(lock.acquireLockMock).toHaveBeenCalledWith( - GET_TOKEN_SILENTLY_LOCK_KEY, - 5000 - ); - expect(lock.releaseLockMock).toHaveBeenCalledWith( - GET_TOKEN_SILENTLY_LOCK_KEY - ); + + expect(lock.acquireLockMock).not.toHaveBeenCalled(); }); it('continues method execution when there is no cache available', async () => { const { auth0, utils } = await setup(); diff --git a/src/Auth0Client.ts b/src/Auth0Client.ts index 4ed5c2123..1452177d2 100644 --- a/src/Auth0Client.ts +++ b/src/Auth0Client.ts @@ -332,8 +332,6 @@ export default class Auth0Client { options.scope = getUniqueScopes(this.DEFAULT_SCOPE, options.scope); try { - await lock.acquireLock(GET_TOKEN_SILENTLY_LOCK_KEY, 5000); - if (!options.ignoreCache) { const cache = this.cache.get({ scope: options.scope, @@ -341,11 +339,12 @@ export default class Auth0Client { }); if (cache) { - await lock.releaseLock(GET_TOKEN_SILENTLY_LOCK_KEY); return cache.access_token; } } + await lock.acquireLock(GET_TOKEN_SILENTLY_LOCK_KEY, 5000); + const stateIn = encodeState(createRandomString()); const nonceIn = createRandomString(); const code_verifier = createRandomString();