Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear axios token after logout #84

Merged
merged 2 commits into from
Mar 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
})
})