Skip to content

Commit

Permalink
No longer acquires a browser lock if there was a hit on the cache (#339)
Browse files Browse the repository at this point in the history
  • Loading branch information
Steve Hobbs authored Jan 24, 2020
1 parent 2b410d1 commit b925472
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 11 deletions.
11 changes: 3 additions & 8 deletions __tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
5 changes: 2 additions & 3 deletions src/Auth0Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,20 +332,19 @@ 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,
audience: options.audience || 'default'
});

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();
Expand Down

0 comments on commit b925472

Please sign in to comment.