diff --git a/lib/module/index.js b/lib/module/index.js index 05d22b666..a48db4183 100644 --- a/lib/module/index.js +++ b/lib/module/index.js @@ -1,6 +1,7 @@ const { resolve, join, basename } = require('path') const { existsSync, readdirSync } = require('fs') const merge = require('lodash/merge') +const uniq = require('lodash/uniq') const defaults = require('./defaults') const libRoot = resolve(__dirname, '..') @@ -16,16 +17,18 @@ module.exports = function (moduleOptions) { copyCore.call(this, options) // Process and normalize strategies - const { strategies, schemes } = processStrategies.call(this, options) + const { strategies, strategyScheme } = processStrategies.call(this, options) delete options.strategies // Set defaultStrategy - if (!options.defaultStrategy) { + if (!options.defaultStrategy && strategies.length) { options.defaultStrategy = strategies[0]._name + } else { + console.warn('no strategy defined!') } // Copy plugin - copyPlugin.call(this, { options, strategies, schemes }) + copyPlugin.call(this, { options, strategies, strategyScheme }) } function validateOptions (options) { @@ -73,15 +76,16 @@ function copyCore (options) { } } -function copyPlugin ({ options, strategies, schemes }) { +function copyPlugin ({ options, strategies, strategyScheme }) { // Copy auth plugin const { dst } = this.addTemplate({ src: resolve(libRoot, 'module', 'plugin.js'), fileName: join('auth', 'plugin.js'), options: { options, - schemes, - strategies + strategies, + uniqueSchemes: uniq([...strategyScheme.values()]), + strategyScheme } }) @@ -96,7 +100,7 @@ function copyPlugin ({ options, strategies, schemes }) { function processStrategies (options) { const strategies = [] - const schemes = {} + const strategyScheme = new Map() for (const name in options.strategies) { if (!options.strategies[name]) { @@ -118,11 +122,9 @@ function processStrategies (options) { // Try to apply provider const provider = resolveProvider.call(this, strategy._provider) + if (typeof provider === 'function') { provider.call(this, strategy, { name }) - strategy._provider = provider.name || '' - } else { - delete strategy._provider } // Guess _scheme property @@ -145,15 +147,17 @@ function processStrategies (options) { fileName: join('auth', 'schemes', schemeName) }) - schemes[schemeName] = '.' + join('/', 'schemes', basename(schemeSrc)) - strategy._scheme = schemeName + // Remove unnecessary fields + delete strategy._scheme + delete strategy._provider + strategyScheme.set(strategy, '.' + join('/', 'schemes', basename(schemeSrc))) strategies.push(strategy) } return { strategies, - schemes + strategyScheme } } diff --git a/lib/module/plugin.js b/lib/module/plugin.js index c2588fa0a..1fff5ba5b 100644 --- a/lib/module/plugin.js +++ b/lib/module/plugin.js @@ -3,7 +3,7 @@ import Auth from './auth' import './middleware' // Active chemes -<%= Object.keys(options.schemes).map(scheme => `import ${'scheme_' + hash(scheme)} from '${options.schemes[scheme]}'`).join('\n') %> +<%= options.uniqueSchemes.map(path =>`import ${'scheme_' + hash(path)} from '${path}'`).join('\n') %> export default function (ctx, inject) { // Options @@ -16,9 +16,15 @@ export default function (ctx, inject) { inject('auth', $auth) // Register strategies - <% for (let strategy of options.strategies) { %> - $auth.registerStrategy('<%= strategy._name %>', new <%='scheme_' + hash(strategy._scheme) %> ($auth, <%= JSON.stringify(strategy) %>)) - <% } %> + + <%= + options.strategies.map(strategy => { + const scheme = 'scheme_' + hash(options.strategyScheme.get(strategy)) + const schemeOptions = JSON.stringify(strategy) + const name = strategy._name + return `// ${name}\n $auth.registerStrategy('${name}', new(${scheme}, ${schemeOptions}))` + }).join('\n\n ') + %> // Initialize auth return $auth.init().catch(error => {