Skip to content

Commit 3712a60

Browse files
author
Pooya Parsa
committed
feat: allow extending auth with plugins (#98)
1 parent f2883c6 commit 3712a60

File tree

4 files changed

+30
-8
lines changed

4 files changed

+30
-8
lines changed

lib/module.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,14 @@ module.exports = function (moduleOptions) {
8888
strategies
8989
}
9090
})
91+
const pluginPath = join(this.options.buildDir, dst)
92+
this.options.plugins.push(pluginPath)
9193

92-
// ...add plugin just after $axios
93-
const index = this.options.plugins.findIndex(p =>
94-
/axios\.js$/.test(p.src || p)
95-
)
96-
this.options.plugins.splice(index + 1, 0, join(this.options.buildDir, dst))
94+
// Extend auth with plugins
95+
if (options.plugins) {
96+
options.plugins.forEach(p => this.options.plugins.push(p))
97+
delete options.plugins
98+
}
9799
}
98100

99101
// -------------------------------------------

test/fixtures/basic/nuxt.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ module.exports = {
55
srcDir: __dirname,
66
serverMiddleware: ['@@/examples/api/auth'],
77
auth: {
8+
plugins: [
9+
'~/plugins/auth.js'
10+
],
811
strategies: {
912
local: {
1013
endpoints: {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function ({ app }) {
2+
app.$auth._custom_plugin = true
3+
}

test/module.test.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ describe('auth', () => {
5656
expect(token).toBeDefined()
5757
expect(user).toBeDefined()
5858
expect(user.username).toBe('test_username')
59+
60+
await page.close()
5961
})
6062

6163
test('logout', async () => {
@@ -80,9 +82,6 @@ describe('auth', () => {
8082
const { logoutToken, logoutAxiosBearer } = await page.evaluate(async () => {
8183
await window.$nuxt.$auth.logout()
8284

83-
// eslint-disable-next-line no-console
84-
console.log('nuxt: ' + window.$nuxt)
85-
8685
return {
8786
logoutAxiosBearer: window.$nuxt.$axios.defaults.headers.common.Authorization,
8887
logoutToken: window.$nuxt.$auth.getToken()
@@ -91,5 +90,20 @@ describe('auth', () => {
9190

9291
expect(logoutToken).toBeNull()
9392
expect(logoutAxiosBearer).toBeUndefined()
93+
94+
await page.close()
95+
})
96+
97+
test('auth plugin', async () => {
98+
const page = await browser.newPage()
99+
await page.goto(url('/'))
100+
101+
const flag = await page.evaluate(async () => {
102+
return window.$nuxt.$auth._custom_plugin
103+
})
104+
105+
expect(flag).toBe(true)
106+
107+
await page.close()
94108
})
95109
})

0 commit comments

Comments
 (0)