Skip to content

Commit

Permalink
split browser and node specific logic for options at build time
Browse files Browse the repository at this point in the history
  • Loading branch information
shuowu committed Jan 17, 2022
1 parent bc6bcee commit 0f3caf6
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 69 deletions.
6 changes: 5 additions & 1 deletion jest.browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ const config = Object.assign({}, baseConfig, {
testPathIgnorePatterns: baseConfig.testPathIgnorePatterns.concat([
'<rootDir>/test/spec/serverStorage.js',
'<rootDir>/test/spec/features/server'
])
]),
moduleNameMapper: Object.assign({}, baseConfig.moduleNameMapper, {
// eslint-disable-next-line no-useless-escape
'^\.\/node$': './browser'
})
});

module.exports = config;
2 changes: 1 addition & 1 deletion lib/crypto/webcrypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
*/


// ./node is swapped for ./browser in webpack config
// ./node is swapped for ./browser at build time for browser specific bundles
export * from './node';
39 changes: 39 additions & 0 deletions lib/options/browser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { StorageManagerOptions } from '../types';

export { default as storage } from '../browser/browserStorage';

export const DEFAULT_STORAGE_MANAGER_OPTIONS: StorageManagerOptions = {
token: {
storageTypes: [
'localStorage',
'sessionStorage',
'cookie'
]
},
cache: {
storageTypes: [
'localStorage',
'sessionStorage',
'cookie'
]
},
transaction: {
storageTypes: [
'sessionStorage',
'localStorage',
'cookie'
]
},
'shared-transaction': {
storageTypes: [
'localStorage'
]
},
'original-uri': {
storageTypes: [
'localStorage'
]
}
};

export const enableSharedStorage = true;
75 changes: 10 additions & 65 deletions lib/options.ts → lib/options/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,66 +12,14 @@


/* eslint-disable complexity */
import { removeTrailingSlash, warn, removeNils } from './util';
import { assertValidConfig } from './builderUtil';
import { OktaAuthOptions, StorageManagerOptions } from './types';
import { removeTrailingSlash, warn, removeNils } from '../util';
import { assertValidConfig } from '../builderUtil';
import { OktaAuthOptions } from '../types';

import fetchRequest from './fetch/fetchRequest';
import browserStorage from './browser/browserStorage';
import serverStorage from './server/serverStorage';
import { isBrowser, isHTTPS } from './features';

const BROWSER_STORAGE: StorageManagerOptions = {
token: {
storageTypes: [
'localStorage',
'sessionStorage',
'cookie'
]
},
cache: {
storageTypes: [
'localStorage',
'sessionStorage',
'cookie'
]
},
transaction: {
storageTypes: [
'sessionStorage',
'localStorage',
'cookie'
]
},
'shared-transaction': {
storageTypes: [
'localStorage'
]
},
'original-uri': {
storageTypes: [
'localStorage'
]
}
};

const SERVER_STORAGE: StorageManagerOptions = {
token: {
storageTypes: [
'memory'
]
},
cache: {
storageTypes: [
'memory'
]
},
transaction: {
storageTypes: [
'memory'
]
}
};
import fetchRequest from '../fetch/fetchRequest';
// ./node is swapped for ./browser at build time for browser specific bundles
import { storage, DEFAULT_STORAGE_MANAGER_OPTIONS, enableSharedStorage } from './node';
import { isBrowser, isHTTPS } from '../features';

function getCookieSettings(args: OktaAuthOptions = {}, isHTTPS: boolean) {
// Secure cookies will be automatically used on a HTTPS connection
Expand Down Expand Up @@ -107,14 +55,11 @@ function getCookieSettings(args: OktaAuthOptions = {}, isHTTPS: boolean) {


export function getDefaultOptions(): OktaAuthOptions {
const storageUtil = isBrowser() ? browserStorage : serverStorage;
const storageManager = isBrowser() ? BROWSER_STORAGE : SERVER_STORAGE;
const enableSharedStorage = isBrowser() ? true : false; // localStorage for multi-tab flows (browser only)
return {
devMode: false,
httpRequestClient: fetchRequest,
storageUtil,
storageManager,
storageUtil: storage,
storageManager: DEFAULT_STORAGE_MANAGER_OPTIONS,
transactionManager: {
enableSharedStorage
}
Expand Down Expand Up @@ -165,7 +110,7 @@ export function buildOptions(args: OktaAuthOptions = {}): OktaAuthOptions {
codeChallengeMethod: args.codeChallengeMethod,
recoveryToken: args.recoveryToken,
activationToken: args.activationToken,

// Give the developer the ability to disable token signature validation.
ignoreSignature: !!args.ignoreSignature,

Expand Down
23 changes: 23 additions & 0 deletions lib/options/node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { StorageManagerOptions } from '../types';

export { default as storage } from '../server/serverStorage';

export const DEFAULT_STORAGE_MANAGER_OPTIONS: StorageManagerOptions = {
token: {
storageTypes: [
'memory'
]
},
cache: {
storageTypes: [
'memory'
]
},
transaction: {
storageTypes: [
'memory'
]
}
};

export const enableSharedStorage = false;
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"license": "Apache-2.0",
"main": "build/cjs/index.js",
"module": "build/esm/index.js",
"browser": "build/dist/okta-auth-js.umd.js",
"types": "build/lib/index.d.ts",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const commonPlugins = [
}),
alias({
entries: [
{ find: /.\/node$/, replacement: './browser' }
{ find: /^\.\/node$/, replacement: './browser' }
]
}),
typescript({
Expand Down
10 changes: 10 additions & 0 deletions test/spec/OktaAuth/constructor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ describe('OktaAuth (constructor)', () => {
},
transaction: {
storageTypes: ['a', 'b']
},
'shared-transaction': {
storageTypes: [
'a'
]
},
'original-uri': {
storageTypes: [
'a'
]
}
};
break;
Expand Down

0 comments on commit 0f3caf6

Please sign in to comment.