From 6206803aa01805e2e1697e41aff5d4cd395d861c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robson=20Ten=C3=B3rio?= Date: Fri, 18 May 2018 05:01:05 -0300 Subject: [PATCH] feat(oauth2): set axios token (#175) --- lib/schemes/oauth2.js | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/lib/schemes/oauth2.js b/lib/schemes/oauth2.js index 6e21a5954..a9f9026a6 100644 --- a/lib/schemes/oauth2.js +++ b/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