Permalink
Browse files

feat(oauth2): set axios token (#175)

  • Loading branch information...
robsontenorio authored and pi0 committed May 18, 2018
1 parent 0602c60 commit 6206803aa01805e2e1697e41aff5d4cd395d861c
Showing with 25 additions and 2 deletions.
  1. +25 −2 lib/schemes/oauth2.js
@@ -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 {
@@ -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()
@@ -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',
@@ -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

0 comments on commit 6206803

Please sign in to comment.