From 1197966a63ef8a09ace90f617d35921c882309df Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Wed, 21 Feb 2018 00:55:13 +0330 Subject: [PATCH] misc: improve token handling with multi token --- lib/auth/auth.js | 50 ++++++++++++++------------------------- lib/auth/schemes/local.js | 29 ++++++++++++++++------- 2 files changed, 39 insertions(+), 40 deletions(-) diff --git a/lib/auth/auth.js b/lib/auth/auth.js index 392c74fa0..5995f3c4d 100644 --- a/lib/auth/auth.js +++ b/lib/auth/auth.js @@ -89,6 +89,8 @@ export default class Auth { // Local Storage this.setLocalStorage(key, value, isJson) + + return value } getUniversal (key, isJson) { @@ -137,6 +139,8 @@ export default class Auth { value }) } + + return value } getState (key) { @@ -168,6 +172,8 @@ export default class Auth { } else { localStorage.setItem(_key, isJson ? JSON.stringify(value) : value) } + + return value } getLocalStorage (key, isJson) { @@ -198,6 +204,8 @@ export default class Auth { } else { Cookies.set(_key, value, _options) } + + return value } getCookie (key, isJson) { @@ -300,44 +308,22 @@ export default class Auth { // Token helpers // --------------------------------------------------------------- - get token () { - return this._state._token - } + getToken (name) { + const _key = '_' + (name || this.options.token.name) - setToken (token) { - if (!this.options.token) { - return - } - - // Keep in private state - this.setState('_token', token) - - // Set Authorization token for all axios requests - this.ctx.app.$axios.setToken(token, this.options.token.type) - - // Save it in cookies - this.setCookie(this.options.cookie.name, token) - - // Save it in localStorage - this.setLocalStorage(this.options.token.name, token) + return this.getUniversal(_key) } - syncToken () { - if (!this.options.token) { - return - } - - let token = this.getState('_token') + setToken (token, name) { + const _key = '_' + (name || this.options.token.name) - if (isUnset(token)) { - token = this.getCookie(this.options.cookie.name) - } + return this.setUniversal(_key, token) + } - if (isUnset(token)) { - token = this.getLocalStorage(this.options.token.name) - } + syncToken (name) { + const _key = '_' + (name || this.options.token.name) - this.setToken(token) + return this.syncUniversal(_key) } // --------------------------------------------------------------- diff --git a/lib/auth/schemes/local.js b/lib/auth/schemes/local.js index 33e87e0a8..b1bb24b23 100644 --- a/lib/auth/schemes/local.js +++ b/lib/auth/schemes/local.js @@ -1,12 +1,21 @@ export default class LocalScheme { constructor (auth, options) { this.auth = auth - this.options = Object.assign({ tokenRequired: true }, options) + this.options = Object.assign( + { tokenRequired: true, tokenType: 'Bearer' }, + options + ) + } + + _setToken (token) { + // Set Authorization token for all axios requests + this.auth.ctx.app.$axios.setToken(token, this.options.tokenType) } mounted () { if (this.options.tokenRequired) { - this.auth.syncToken() + const token = this.auth.syncToken() + this._setToken(token) } } @@ -15,10 +24,12 @@ export default class LocalScheme { return Promise.resolve() } - return this.auth.request(endpoint, this.options.endpoints.login) - .then(data => { + return this.auth + .request(endpoint, this.options.endpoints.login) + .then(token => { if (this.options.tokenRequired) { - this.auth.setToken(data) + this.auth.setToken(token) + this._setToken(token) } }) .then(() => this.fetchUser()) @@ -37,7 +48,8 @@ export default class LocalScheme { } // Try to fetch user and then set loggedIn to true - return this.auth.request(endpoint, this.options.endpoints.user) + return this.auth + .request(endpoint, this.options.endpoints.user) .then(user => this.auth.setUser(user)) } @@ -46,7 +58,8 @@ export default class LocalScheme { return Promise.resolve() } - return this.auth.request(endpoint, this.options.endpoints.logout) - .catch(() => { }) + return this.auth + .request(endpoint, this.options.endpoints.logout) + .catch(() => {}) } }