Skip to content

Commit

Permalink
misc(auth): improve storage
Browse files Browse the repository at this point in the history
  • Loading branch information
Pooya Parsa committed Feb 20, 2018
1 parent 2a4898a commit 272ee05
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 31 deletions.
72 changes: 49 additions & 23 deletions lib/auth/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import Cookies from 'js-cookie'
import { parse as parseCookie } from 'cookie'
import getProp from 'dotprop'
import Vue from 'vue'
import { routeOption, isRelativeURL, isUnset, isSet, isSameURL } from './utilities'
import {
routeOption,
isRelativeURL,
isUnset,
isSet,
isSameURL
} from './utilities'

export default class Auth {
constructor (ctx, options) {
Expand Down Expand Up @@ -55,8 +61,8 @@ export default class Auth {
}
}

this.ctx.store.registerModule(this.options.namespace, authModule, {
preserveState: Boolean(this.ctx.store.state[this.options.namespace])
this.ctx.store.registerModule(this.options.vuex.namespace, authModule, {
preserveState: Boolean(this.ctx.store.state[this.options.vuex.namespace])
})
}

Expand Down Expand Up @@ -119,14 +125,17 @@ export default class Auth {
// ...Vuex

get state () {
return this.ctx.store.state[this.options.namespace]
return this.ctx.store.state[this.options.vuex.namespace]
}

setState (key, value) {
if (key[0] === '_') {
Vue.set(this._state, key, value)
} else {
this.ctx.store.commit(this.options.namespace + '/SET', { key, value })
this.ctx.store.commit(this.options.vuex.namespace + '/SET', {
key,
value
})
}
}

Expand All @@ -140,28 +149,37 @@ export default class Auth {

watchState (key, fn) {
return this.ctx.store.watch(
state => getProp(state[this.options.namespace], key),
state => getProp(state[this.options.vuex.namespace], key),
fn
)
}

// ...Local Storage

setLocalStorage (key, value, isJson) {
if (typeof localStorage !== 'undefined') {
if (isUnset(value)) {
localStorage.removeItem(key)
} else {
localStorage.setItem(key, isJson ? JSON.stringify(value) : value)
}
if (typeof localStorage === 'undefined' || !this.options.localStorage) {
return
}

const _key = this.options.localStorage.prefix + key

if (isUnset(value)) {
localStorage.removeItem(_key)
} else {
localStorage.setItem(_key, isJson ? JSON.stringify(value) : value)
}
}

getLocalStorage (key, isJson) {
if (typeof localStorage !== 'undefined') {
const value = localStorage.getItem(key)
return isJson ? JSON.parse(value) : value
if (typeof localStorage === 'undefined' || !this.options.localStorage) {
return
}

const _key = this.options.localStorage.prefix + key

const value = localStorage.getItem(_key)

return isJson ? JSON.parse(value) : value
}

// ...Cookies
Expand All @@ -171,12 +189,14 @@ export default class Auth {
return
}

const _key = this.options.cookie.prefix + key

const _options = Object.assign({}, this.options.cookie.options, options)

if (isUnset(value)) {
Cookies.remove(key, _options)
Cookies.remove(_key, _options)
} else {
Cookies.set(key, value, _options)
Cookies.set(_key, value, _options)
}
}

Expand All @@ -185,12 +205,14 @@ export default class Auth {
return
}

const _key = this.options.cookie.prefix + key

const cookieStr = process.browser
? document.cookie
: this.ctx.req.headers.cookie

const cookies = parseCookie(cookieStr || '') || {}
const value = cookies[key]
const value = cookies[_key]

return isJson ? JSON.parse(value) : value
}
Expand Down Expand Up @@ -224,15 +246,16 @@ export default class Auth {

mounted () {
if (this.strategy.mounted) {
return Promise.resolve(this.strategy.mounted(...arguments)).then(() => this.fetchUserOnce())
return Promise.resolve(this.strategy.mounted(...arguments)).then(() =>
this.fetchUserOnce()
)
}

return this.fetchUserOnce()
}

loginWith (name, ...args) {
return this.setStrategy(name)
.then(() => this.login(...args))
return this.setStrategy(name).then(() => this.login(...args))
}

login () {
Expand All @@ -253,7 +276,9 @@ export default class Auth {

logout () {
if (this.strategy.logout) {
return Promise.resolve(this.strategy.logout(...arguments)).then(() => this.reset())
return Promise.resolve(this.strategy.logout(...arguments)).then(() =>
this.reset()
)
}

this.reset()
Expand Down Expand Up @@ -403,7 +428,8 @@ export default class Auth {
}

hasScope (scope) {
const userScopes = this.state.user && getProp(this.state.user, this.options.scopeKey)
const userScopes =
this.state.user && getProp(this.state.user, this.options.scopeKey)

if (!userScopes) {
return undefined
Expand Down
46 changes: 38 additions & 8 deletions lib/defaults.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,62 @@
module.exports = {
// Error handling

resetOnError: false,
rewriteRedirects: true,
namespace: 'auth',

// Authorization

scopeKey: 'scope',
defaultStrategy: undefined, /* will be auto set at module level */

// Redirects

rewriteRedirects: true,

redirect: {
login: '/login',
logout: '/',
home: '/'
},

token: {
type: 'Bearer',
name: 'token'
// Vuex Store

vuex: {
namespace: 'auth'
},

// Cookie Store

cookie: {
name: 'token',
prefix: 'auth_',
options: {
path: '/'
}
},

// localStorage Store

localStorage: {
prefix: 'auth::'
},

// Token

token: {
type: 'Bearer',
name: 'token'
},

// Strategies

defaultStrategy: undefined /* will be auto set at module level */,

strategies: {
local: {
endpoints: {
login: { url: '/api/auth/login', method: 'post', propertyName: 'token' },
login: {
url: '/api/auth/login',
method: 'post',
propertyName: 'token'
},
logout: { url: '/api/auth/logout', method: 'post' },
user: { url: '/api/auth/user', method: 'get', propertyName: 'user' }
}
Expand Down

0 comments on commit 272ee05

Please sign in to comment.