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

Oauth2: set axios token #175

Merged
merged 2 commits into from
May 18, 2018
Merged
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
27 changes: 25 additions & 2 deletions lib/schemes/oauth2.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { encodeQuery, parseQuery, randomString } from '../utilities'

const DEFAULTS = {
token_type: 'Bearer',
response_type: 'token'
response_type: 'token',
tokenName: 'Authorization'
}

export default class Oauth2Scheme {
Expand Down Expand Up @@ -33,7 +34,11 @@ export default class Oauth2Scheme {

async mounted () {
// Sync token
this.$auth.syncToken(this.name)
const token = this.$auth.syncToken(this.name)
// Set axios token
if (token) {
this._setToken(token)
}

// Handle callbacks on page load
const redirected = await this._handleCallback()
Expand All @@ -43,6 +48,21 @@ export default class Oauth2Scheme {
}
}

_setToken (token) {
// Set Authorization token for all axios requests
this.$auth.ctx.app.$axios.setHeader(this.options.tokenName, token)
}

_clearToken () {
// Clear Authorization token for all axios requests
this.$auth.ctx.app.$axios.setHeader(this.options.tokenName, false)
}

async logout () {
this._clearToken()
return this.$auth.reset()
}

login () {
const opts = {
protocol: 'oauth2',
Expand Down Expand Up @@ -137,6 +157,9 @@ export default class Oauth2Scheme {
// Store token
this.$auth.setToken(this.name, token)

// Set axios token
this._setToken(token)

// Store refresh token
if (refreshToken && refreshToken.length) {
refreshToken = this.options.token_type + ' ' + refreshToken
Expand Down