Skip to content

Commit be65f09

Browse files
dfcookpi0
authored andcommitted
fix: clear axios token after logout (#84)
* Clear axios token after logout * Add test coverage
1 parent 4f848cb commit be65f09

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

lib/auth/schemes/local.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ export default class LocalScheme {
1919
}
2020
}
2121

22+
_clearToken () {
23+
if (this.options.globalToken) {
24+
// Clear Authorization token for all axios requests
25+
this.auth.ctx.app.$axios.setToken(false)
26+
}
27+
}
28+
2229
mounted () {
2330
if (this.options.tokenRequired) {
2431
const token = this.auth.syncToken(this.name)
@@ -80,6 +87,10 @@ export default class LocalScheme {
8087
.requestWith(this.name, endpoint, this.options.endpoints.logout)
8188
.catch(() => { })
8289

90+
if (this.options.tokenRequired) {
91+
this._clearToken()
92+
}
93+
8394
return this.auth.reset()
8495
}
8596
}

test/module.test.js

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,55 @@ describe('auth', () => {
3939
await page.goto(url('/'))
4040
await page.waitForFunction('!!window.$nuxt')
4141

42-
const { token, user } = await page.evaluate(async () => {
42+
const { token, user, axiosBearer } = await page.evaluate(async () => {
4343
await window.$nuxt.$auth.loginWith('local', {
4444
data: { username: 'test_username', password: '123' }
4545
})
4646

4747
return {
48+
axiosBearer: window.$nuxt.$axios.defaults.headers.common.Authorization,
4849
token: window.$nuxt.$auth.getToken(),
4950
user: window.$nuxt.$auth.state.user
5051
}
5152
})
5253

54+
expect(axiosBearer).toBeDefined()
5355
expect(token).toBeDefined()
5456
expect(user.username).toBe('test_username')
5557
})
58+
59+
test('logout', async () => {
60+
const page = await browser.newPage()
61+
await page.goto(url('/'))
62+
await page.waitForFunction('!!window.$nuxt')
63+
64+
const { loginAxiosBearer, loginToken } = await page.evaluate(async () => {
65+
await window.$nuxt.$auth.loginWith('local', {
66+
data: { username: 'test_username', password: '123' }
67+
})
68+
69+
return {
70+
loginAxiosBearer: window.$nuxt.$axios.defaults.headers.common.Authorization,
71+
loginToken: window.$nuxt.$auth.getToken()
72+
}
73+
})
74+
75+
expect(loginAxiosBearer).toBeDefined()
76+
expect(loginToken).toBeDefined()
77+
78+
const { logoutToken, logoutAxiosBearer } = await page.evaluate(async () => {
79+
await window.$nuxt.$auth.logout()
80+
81+
// eslint-disable-next-line no-console
82+
console.log('nuxt: ' + window.$nuxt)
83+
84+
return {
85+
logoutAxiosBearer: window.$nuxt.$axios.defaults.headers.common.Authorization,
86+
logoutToken: window.$nuxt.$auth.getToken()
87+
}
88+
})
89+
90+
expect(logoutToken).toBeNull()
91+
expect(logoutAxiosBearer).toBeUndefined()
92+
})
5693
})

0 commit comments

Comments
 (0)