From 23a9452f592e6f6e85cee373ade84149784f7e7b Mon Sep 17 00:00:00 2001 From: adamjmcgrath Date: Thu, 19 Mar 2020 16:28:15 +0000 Subject: [PATCH 1/2] Revert "Revert "[SDK-1379] Export constructor" (#382)" This reverts commit 178b65f30c9b7a76ce762a1b4de1bd3ccfca5e5a. # Conflicts: # __tests__/index.test.ts # src/Auth0Client.ts # src/index.ts --- README.md | 16 ++++++++++++++++ __tests__/index.test.ts | 14 ++++++-------- cypress/integration/initialisation.js | 19 +++++++++++++++++++ package.json | 1 - rollup.config.js | 12 ++++++++---- src/Auth0Client.ts | 4 +++- src/index.cjs.ts | 6 ++++++ src/index.ts | 9 +++------ 8 files changed, 61 insertions(+), 20 deletions(-) create mode 100644 cypress/integration/initialisation.js create mode 100644 src/index.cjs.ts diff --git a/README.md b/README.md index b04177290..e37886be8 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,22 @@ createAuth0Client({ }).then(auth0 => { //... }); + +//or, you can just instantiate the client on it's own +import { Auth0Client } from '@auth0/auth0-spa-js'; +const auth0 = new Auth0Client({ + domain: '', + client_id: '', + redirect_uri: '' +}); +//if you do this, you'll need to check the session yourself +try { + await getTokenSilently(); +} catch (error) { + if (error.error !== 'login_required') { + throw error; + } +} ``` ### 1 - Login diff --git a/__tests__/index.test.ts b/__tests__/index.test.ts index 79e161ec0..ac5da3b3e 100644 --- a/__tests__/index.test.ts +++ b/__tests__/index.test.ts @@ -5,9 +5,11 @@ jest.mock('../src/transaction-manager'); jest.mock('../src/utils'); import { CacheLocation } from '../src/global'; -import Auth0Client from '../src/Auth0Client'; -import createAuth0Client, { GetTokenSilentlyOptions } from '../src/index'; +import createAuth0Client, { + Auth0Client, + GetTokenSilentlyOptions +} from '../src/index'; import { AuthenticationError } from '../src/errors'; import version from '../src/version'; @@ -351,9 +353,7 @@ describe('Auth0', () => { it('opens popup with correct popup, url and custom config', async () => { const { auth0, utils } = await setup(); await auth0.loginWithPopup({}, { timeoutInSeconds: 1 }); - expect( - utils.runPopup - ).toHaveBeenCalledWith( + expect(utils.runPopup).toHaveBeenCalledWith( `https://test.auth0.com/authorize?${TEST_QUERY_PARAMS}${TEST_TELEMETRY_QUERY_STRING}`, { timeoutInSeconds: 1 } ); @@ -369,9 +369,7 @@ describe('Auth0', () => { } ); - expect( - utils.runPopup - ).toHaveBeenCalledWith( + expect(utils.runPopup).toHaveBeenCalledWith( `https://test.auth0.com/authorize?${TEST_QUERY_PARAMS}${TEST_TELEMETRY_QUERY_STRING}`, { timeoutInSeconds: 25 } ); diff --git a/cypress/integration/initialisation.js b/cypress/integration/initialisation.js new file mode 100644 index 000000000..5d9dbb07b --- /dev/null +++ b/cypress/integration/initialisation.js @@ -0,0 +1,19 @@ +import { whenReady } from '../support/utils'; + +describe('initialisation', function() { + beforeEach(cy.resetTests); + + it('should expose a factory method and constructor', function(done) { + whenReady().then(win => { + assert.isFunction( + win.createAuth0Client, + 'The createAuth0Client function should be declared on the window.' + ); + assert.isFunction( + win.Auth0Client, + 'The Auth0Client constructor should be declared on the window.' + ); + done(); + }); + }); +}); diff --git a/package.json b/package.json index cd0d18524..35af15b91 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,6 @@ "version": "1.7.0-beta.4", "main": "dist/lib/auth0-spa-js.cjs.js", "types": "dist/typings/index.d.ts", - "browser": "dist/auth0-spa-js.production.js", "module": "dist/auth0-spa-js.production.esm.js", "scripts": { "dev": "rimraf dist && rollup -c --watch", diff --git a/rollup.config.js b/rollup.config.js index 2063fa0e0..b03b14534 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -34,13 +34,16 @@ const getPlugins = shouldMinify => { sourcemaps() ]; }; +const footer = `('Auth0Client' in this) && this.console && this.console.warn && this.console.warn('Auth0Client already declared on the global namespace'); +this && this.${EXPORT_NAME} && (this.Auth0Client = this.Auth0Client || this.${EXPORT_NAME}.Auth0Client);`; let bundles = [ { - input: 'src/index.ts', + input: 'src/index.cjs.ts', output: { name: EXPORT_NAME, file: 'dist/auth0-spa-js.development.js', + footer, format: 'umd' }, plugins: [ @@ -62,11 +65,12 @@ let bundles = [ if (isProduction) { bundles = bundles.concat( { - input: 'src/index.ts', + input: 'src/index.cjs.ts', output: [ { name: EXPORT_NAME, - file: pkg.browser, + file: 'dist/auth0-spa-js.production.js', + footer, format: 'umd' } ], @@ -86,7 +90,7 @@ if (isProduction) { plugins: getPlugins(isProduction) }, { - input: 'src/index.ts', + input: 'src/index.cjs.ts', output: [ { name: EXPORT_NAME, diff --git a/src/Auth0Client.ts b/src/Auth0Client.ts index 548da7dca..924fc7161 100644 --- a/src/Auth0Client.ts +++ b/src/Auth0Client.ts @@ -10,7 +10,8 @@ import { runIframe, sha256, bufferToBase64UrlEncoded, - oauthToken + oauthToken, + validateCrypto } from './utils'; import { InMemoryCache, ICache, LocalStorageCache } from './cache'; @@ -79,6 +80,7 @@ export default class Auth0Client { cacheLocation: CacheLocation; constructor(private options: Auth0ClientOptions) { + validateCrypto(); this.cacheLocation = options.cacheLocation || 'memory'; if (!cacheFactory(this.cacheLocation)) { diff --git a/src/index.cjs.ts b/src/index.cjs.ts new file mode 100644 index 000000000..0efc19df9 --- /dev/null +++ b/src/index.cjs.ts @@ -0,0 +1,6 @@ +import createAuth0Client, { Auth0Client } from './index'; + +export default Object.assign(createAuth0Client, { + Auth0Client, + createAuth0Client +}); diff --git a/src/index.ts b/src/index.ts index 260eaf320..79eb86b66 100644 --- a/src/index.ts +++ b/src/index.ts @@ -7,25 +7,22 @@ import 'promise-polyfill/src/polyfill'; import 'fast-text-encoding'; import 'abortcontroller-polyfill/dist/abortcontroller-polyfill-only'; -import Auth0Client_ from './Auth0Client'; +import Auth0Client from './Auth0Client'; import * as ClientStorage from './storage'; import { Auth0ClientOptions } from './global'; import { CACHE_LOCATION_MEMORY } from './constants'; import './global'; -import { validateCrypto } from './utils'; import { getUniqueScopes } from './utils'; export * from './global'; export default async function createAuth0Client(options: Auth0ClientOptions) { - validateCrypto(); - if (options.useRefreshTokens) { options.scope = getUniqueScopes(options.scope, 'offline_access'); } - const auth0 = new Auth0Client_(options); + const auth0 = new Auth0Client(options); if ( auth0.cacheLocation === CACHE_LOCATION_MEMORY && @@ -45,4 +42,4 @@ export default async function createAuth0Client(options: Auth0ClientOptions) { return auth0; } -export type Auth0Client = Auth0Client_; +export { Auth0Client }; From aec02285740e58443212dc7e676affef74488d13 Mon Sep 17 00:00:00 2001 From: adamjmcgrath Date: Thu, 19 Mar 2020 16:34:41 +0000 Subject: [PATCH 2/2] rever whitespace change