Skip to content

Commit

Permalink
feat: allow extending auth with plugins (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pooya Parsa committed Apr 1, 2018
1 parent f2883c6 commit 3712a60
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 8 deletions.
12 changes: 7 additions & 5 deletions lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

// -------------------------------------------
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/basic/nuxt.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ module.exports = {
srcDir: __dirname,
serverMiddleware: ['@@/examples/api/auth'],
auth: {
plugins: [
'~/plugins/auth.js'
],
strategies: {
local: {
endpoints: {
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/basic/plugins/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function ({ app }) {
app.$auth._custom_plugin = true
}
20 changes: 17 additions & 3 deletions test/module.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ describe('auth', () => {
expect(token).toBeDefined()
expect(user).toBeDefined()
expect(user.username).toBe('test_username')

await page.close()
})

test('logout', async () => {
Expand All @@ -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()
Expand All @@ -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.