Skip to content

Commit

Permalink
[SDK-1352] Stop checking isAuthenticated cookie on initialization w…
Browse files Browse the repository at this point in the history
…hen using local storage (#352)

* Changes to the initialization strategy

* Removed unused import from a test
  • Loading branch information
Steve Hobbs authored Feb 17, 2020
1 parent 966f6a2 commit ecff936
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 33 deletions.
50 changes: 32 additions & 18 deletions __tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ jest.mock('../src/transaction-manager');
jest.mock('../src/utils');

import Auth0Client from '../src/Auth0Client';
import createAuth0Client, {
PopupConfigOptions,
GetTokenSilentlyOptions
} from '../src/index';
import { CacheLocation } from '../src/global';

import createAuth0Client, { GetTokenSilentlyOptions } from '../src/index';

import { AuthenticationError } from '../src/errors';
import version from '../src/version';
Expand Down Expand Up @@ -55,7 +54,8 @@ const mockEnclosedCache = {
jest.mock('../src/cache', () => ({
InMemoryCache: () => ({
enclosedCache: mockEnclosedCache
})
}),
LocalStorageCache: () => mockEnclosedCache
}));

const setup = async (options = {}) => {
Expand Down Expand Up @@ -1913,14 +1913,11 @@ describe('default creation function', () => {
client_id: TEST_CLIENT_ID
});

expect(auth0.getTokenSilently).toHaveBeenCalledWith({
audience: undefined,
ignoreCache: true
});
expect(auth0.getTokenSilently).toHaveBeenCalledWith();
});

describe('when refresh tokens are not used', () => {
it('calls getTokenSilently with audience and scope', async () => {
it('calls getTokenSilently', async () => {
const utils = require('../src/utils');

const options = {
Expand All @@ -1939,10 +1936,7 @@ describe('default creation function', () => {
...options
});

expect(auth0.getTokenSilently).toHaveBeenCalledWith({
ignoreCache: true,
...options
});
expect(auth0.getTokenSilently).toHaveBeenCalledWith();
});
});

Expand Down Expand Up @@ -1972,11 +1966,31 @@ describe('default creation function', () => {
'offline_access'
);

expect(auth0.getTokenSilently).toHaveBeenCalledWith({
ignoreCache: true,
scope: 'the-scope offline_access',
audience: 'the-audience'
expect(auth0.getTokenSilently).toHaveBeenCalledWith();
});
});

describe('when localstorage is used', () => {
it('refreshes token state regardless of isauthenticated cookie', async () => {
const cacheLocation: CacheLocation = 'localstorage';

const options = {
audience: 'the-audience',
scope: 'the-scope',
cacheLocation
};

Auth0Client.prototype.getTokenSilently = jest.fn();

require('../src/storage').get = () => false;

const auth0 = await createAuth0Client({
domain: TEST_DOMAIN,
client_id: TEST_CLIENT_ID,
...options
});

expect(auth0.getTokenSilently).toHaveBeenCalledWith();
});
});
});
20 changes: 12 additions & 8 deletions src/Auth0Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ import {
GetTokenWithPopupOptions,
LogoutOptions,
RefreshTokenOptions,
OAuthTokenOptions
OAuthTokenOptions,
CacheLocation
} from './global';

/**
Expand All @@ -54,13 +55,16 @@ const GET_TOKEN_SILENTLY_LOCK_KEY = 'auth0.lock.getTokenSilently';
/**
* @ignore
*/
const cacheFactory = location => {
const builders = {
memory: () => new InMemoryCache().enclosedCache,
localstorage: () => new LocalStorageCache()
};
const cacheLocationBuilders = {
memory: () => new InMemoryCache().enclosedCache,
localstorage: () => new LocalStorageCache()
};

return builders[location];
/**
* @ignore
*/
const cacheFactory = (location: string) => {
return cacheLocationBuilders[location];
};

/**
Expand All @@ -73,7 +77,7 @@ export default class Auth0Client {
private tokenIssuer: string;
private readonly DEFAULT_SCOPE = 'openid profile email';

cacheLocation: string;
cacheLocation: CacheLocation;

constructor(private options: Auth0ClientOptions) {
this.cacheLocation = options.cacheLocation || 'memory';
Expand Down
3 changes: 3 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@ export const DEFAULT_SILENT_TOKEN_RETRY_COUNT = 3;
* @ignore
*/
export const DEFAULT_FETCH_TIMEOUT_MS = 10000;

export const CACHE_LOCATION_MEMORY = 'memory';
export const CACHE_LOCATION_LOCAL_STORAGE = 'localstorage';
7 changes: 6 additions & 1 deletion src/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export interface Auth0ClientOptions extends BaseLoginOptions {
* The location to use when storing cache data. Valid values are `memory` or `localstorage`.
* The default setting is `memory`.
*/
cacheLocation?: 'memory' | 'localstorage';
cacheLocation?: CacheLocation;

/**
* If true, refresh tokens are used to fetch new access tokens from the Auth0 server. If false, the legacy technique of using a hidden iframe and the `authorization_code` grant with `prompt=none` is used.
Expand All @@ -112,6 +112,11 @@ export interface Auth0ClientOptions extends BaseLoginOptions {
authorizeTimeoutInSeconds?: number;
}

/**
* The possible locations where tokens can be stored
*/
export type CacheLocation = 'memory' | 'localstorage';

/**
* @ignore
*/
Expand Down
13 changes: 7 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import 'abortcontroller-polyfill/dist/abortcontroller-polyfill-only';
import Auth0Client from './Auth0Client';
import * as ClientStorage from './storage';
import { Auth0ClientOptions } from './global';
import { CACHE_LOCATION_MEMORY } from './constants';

import './global';

import { validateCrypto, getUniqueScopes } from './utils';
Expand All @@ -26,16 +28,15 @@ export default async function createAuth0Client(options: Auth0ClientOptions) {

const auth0 = new Auth0Client(options);

if (!ClientStorage.get('auth0.is.authenticated')) {
if (
auth0.cacheLocation === CACHE_LOCATION_MEMORY &&
!ClientStorage.get('auth0.is.authenticated')
) {
return auth0;
}

try {
await auth0.getTokenSilently({
audience: options.audience,
scope: options.scope,
ignoreCache: true
});
await auth0.getTokenSilently();
} catch (error) {
// ignore
}
Expand Down

0 comments on commit ecff936

Please sign in to comment.