Skip to content

Commit

Permalink
misc: improve token handling with multi token
Browse files Browse the repository at this point in the history
  • Loading branch information
Pooya Parsa committed Feb 20, 2018
1 parent 272ee05 commit 1197966
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 40 deletions.
50 changes: 18 additions & 32 deletions lib/auth/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ export default class Auth {

// Local Storage
this.setLocalStorage(key, value, isJson)

return value
}

getUniversal (key, isJson) {
Expand Down Expand Up @@ -137,6 +139,8 @@ export default class Auth {
value
})
}

return value
}

getState (key) {
Expand Down Expand Up @@ -168,6 +172,8 @@ export default class Auth {
} else {
localStorage.setItem(_key, isJson ? JSON.stringify(value) : value)
}

return value
}

getLocalStorage (key, isJson) {
Expand Down Expand Up @@ -198,6 +204,8 @@ export default class Auth {
} else {
Cookies.set(_key, value, _options)
}

return value
}

getCookie (key, isJson) {
Expand Down Expand Up @@ -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)
}

// ---------------------------------------------------------------
Expand Down
29 changes: 21 additions & 8 deletions lib/auth/schemes/local.js
Original file line number Diff line number Diff line change
@@ -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)
}
}

Expand All @@ -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())
Expand All @@ -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))
}

Expand All @@ -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(() => {})
}
}

0 comments on commit 1197966

Please sign in to comment.