From 3712a6035e60655234846dd22fdb78e004377feb Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Sun, 1 Apr 2018 20:56:06 +0430 Subject: [PATCH] feat: allow extending auth with plugins (#98) --- lib/module.js | 12 +++++++----- test/fixtures/basic/nuxt.config.js | 3 +++ test/fixtures/basic/plugins/auth.js | 3 +++ test/module.test.js | 20 +++++++++++++++++--- 4 files changed, 30 insertions(+), 8 deletions(-) create mode 100644 test/fixtures/basic/plugins/auth.js diff --git a/lib/module.js b/lib/module.js index 7a2737f7d..eee428728 100644 --- a/lib/module.js +++ b/lib/module.js @@ -88,12 +88,14 @@ module.exports = function (moduleOptions) { strategies } }) + const pluginPath = join(this.options.buildDir, dst) + this.options.plugins.push(pluginPath) - // ...add plugin just after $axios - const index = this.options.plugins.findIndex(p => - /axios\.js$/.test(p.src || p) - ) - this.options.plugins.splice(index + 1, 0, join(this.options.buildDir, dst)) + // Extend auth with plugins + if (options.plugins) { + options.plugins.forEach(p => this.options.plugins.push(p)) + delete options.plugins + } } // ------------------------------------------- diff --git a/test/fixtures/basic/nuxt.config.js b/test/fixtures/basic/nuxt.config.js index 8313b83af..516d48452 100644 --- a/test/fixtures/basic/nuxt.config.js +++ b/test/fixtures/basic/nuxt.config.js @@ -5,6 +5,9 @@ module.exports = { srcDir: __dirname, serverMiddleware: ['@@/examples/api/auth'], auth: { + plugins: [ + '~/plugins/auth.js' + ], strategies: { local: { endpoints: { diff --git a/test/fixtures/basic/plugins/auth.js b/test/fixtures/basic/plugins/auth.js new file mode 100644 index 000000000..6cdfbeac6 --- /dev/null +++ b/test/fixtures/basic/plugins/auth.js @@ -0,0 +1,3 @@ +export default function ({ app }) { + app.$auth._custom_plugin = true +} diff --git a/test/module.test.js b/test/module.test.js index 7519b0b7f..a8a894e44 100644 --- a/test/module.test.js +++ b/test/module.test.js @@ -56,6 +56,8 @@ describe('auth', () => { expect(token).toBeDefined() expect(user).toBeDefined() expect(user.username).toBe('test_username') + + await page.close() }) test('logout', async () => { @@ -80,9 +82,6 @@ describe('auth', () => { const { logoutToken, logoutAxiosBearer } = await page.evaluate(async () => { await window.$nuxt.$auth.logout() - // eslint-disable-next-line no-console - console.log('nuxt: ' + window.$nuxt) - return { logoutAxiosBearer: window.$nuxt.$axios.defaults.headers.common.Authorization, logoutToken: window.$nuxt.$auth.getToken() @@ -91,5 +90,20 @@ describe('auth', () => { expect(logoutToken).toBeNull() expect(logoutAxiosBearer).toBeUndefined() + + await page.close() + }) + + test('auth plugin', async () => { + const page = await browser.newPage() + await page.goto(url('/')) + + const flag = await page.evaluate(async () => { + return window.$nuxt.$auth._custom_plugin + }) + + expect(flag).toBe(true) + + await page.close() }) })