Skip to content

Commit

Permalink
fix: clear axios token after logout (#84)
Browse files Browse the repository at this point in the history
* Clear axios token after logout

* Add test coverage
  • Loading branch information
dfcook authored and pi0 committed Mar 5, 2018
1 parent 4f848cb commit be65f09
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
11 changes: 11 additions & 0 deletions lib/auth/schemes/local.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ export default class LocalScheme {
}
}

_clearToken () {
if (this.options.globalToken) {
// Clear Authorization token for all axios requests
this.auth.ctx.app.$axios.setToken(false)
}
}

mounted () {
if (this.options.tokenRequired) {
const token = this.auth.syncToken(this.name)
Expand Down Expand Up @@ -80,6 +87,10 @@ export default class LocalScheme {
.requestWith(this.name, endpoint, this.options.endpoints.logout)
.catch(() => { })

if (this.options.tokenRequired) {
this._clearToken()
}

return this.auth.reset()
}
}
39 changes: 38 additions & 1 deletion test/module.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,55 @@ describe('auth', () => {
await page.goto(url('/'))
await page.waitForFunction('!!window.$nuxt')

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

return {
axiosBearer: window.$nuxt.$axios.defaults.headers.common.Authorization,
token: window.$nuxt.$auth.getToken(),
user: window.$nuxt.$auth.state.user
}
})

expect(axiosBearer).toBeDefined()
expect(token).toBeDefined()
expect(user.username).toBe('test_username')
})

test('logout', async () => {
const page = await browser.newPage()
await page.goto(url('/'))
await page.waitForFunction('!!window.$nuxt')

const { loginAxiosBearer, loginToken } = await page.evaluate(async () => {
await window.$nuxt.$auth.loginWith('local', {
data: { username: 'test_username', password: '123' }
})

return {
loginAxiosBearer: window.$nuxt.$axios.defaults.headers.common.Authorization,
loginToken: window.$nuxt.$auth.getToken()
}
})

expect(loginAxiosBearer).toBeDefined()
expect(loginToken).toBeDefined()

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()
}
})

expect(logoutToken).toBeNull()
expect(logoutAxiosBearer).toBeUndefined()
})
})

0 comments on commit be65f09

Please sign in to comment.