Permalink
Browse files

feat: allow extending auth with plugins (#98)

  • Loading branch information...
pi0 committed Apr 1, 2018
1 parent f2883c6 commit 3712a6035e60655234846dd22fdb78e004377feb
Showing with 30 additions and 8 deletions.
  1. +7 −5 lib/module.js
  2. +3 −0 test/fixtures/basic/nuxt.config.js
  3. +3 −0 test/fixtures/basic/plugins/auth.js
  4. +17 −3 test/module.test.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
}
}
// -------------------------------------------
@@ -5,6 +5,9 @@ module.exports = {
srcDir: __dirname,
serverMiddleware: ['@@/examples/api/auth'],
auth: {
plugins: [
'~/plugins/auth.js'
],
strategies: {
local: {
endpoints: {
@@ -0,0 +1,3 @@
export default function ({ app }) {
app.$auth._custom_plugin = true
}
@@ -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()
})
})

0 comments on commit 3712a60

Please sign in to comment.