Skip to content

Commit 6206803

Browse files
robsontenoriopi0
authored andcommitted
feat(oauth2): set axios token (#175)
1 parent 0602c60 commit 6206803

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

lib/schemes/oauth2.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import { encodeQuery, parseQuery, randomString } from '../utilities'
22

33
const DEFAULTS = {
44
token_type: 'Bearer',
5-
response_type: 'token'
5+
response_type: 'token',
6+
tokenName: 'Authorization'
67
}
78

89
export default class Oauth2Scheme {
@@ -33,7 +34,11 @@ export default class Oauth2Scheme {
3334

3435
async mounted () {
3536
// Sync token
36-
this.$auth.syncToken(this.name)
37+
const token = this.$auth.syncToken(this.name)
38+
// Set axios token
39+
if (token) {
40+
this._setToken(token)
41+
}
3742

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

51+
_setToken (token) {
52+
// Set Authorization token for all axios requests
53+
this.$auth.ctx.app.$axios.setHeader(this.options.tokenName, token)
54+
}
55+
56+
_clearToken () {
57+
// Clear Authorization token for all axios requests
58+
this.$auth.ctx.app.$axios.setHeader(this.options.tokenName, false)
59+
}
60+
61+
async logout () {
62+
this._clearToken()
63+
return this.$auth.reset()
64+
}
65+
4666
login () {
4767
const opts = {
4868
protocol: 'oauth2',
@@ -137,6 +157,9 @@ export default class Oauth2Scheme {
137157
// Store token
138158
this.$auth.setToken(this.name, token)
139159

160+
// Set axios token
161+
this._setToken(token)
162+
140163
// Store refresh token
141164
if (refreshToken && refreshToken.length) {
142165
refreshToken = this.options.token_type + ' ' + refreshToken

0 commit comments

Comments
 (0)