Skip to content

Commit

Permalink
feat(oauth2): set axios token (#175)
Browse files Browse the repository at this point in the history
  • Loading branch information
robsontenorio authored and pi0 committed May 18, 2018
1 parent 0602c60 commit 6206803
Showing 1 changed file with 25 additions and 2 deletions.
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

0 comments on commit 6206803

Please sign in to comment.