forked from nuxt-community/auth-module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.js
206 lines (161 loc) Β· 4.48 KB
/
storage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import Vue from 'vue'
import Cookies from 'js-cookie'
import getProp from 'dotprop'
import { parse as parseCookie } from 'cookie'
import { isUnset, isSet } from './utilities'
export default class Storage {
constructor (ctx, options) {
this.ctx = ctx
this.options = options
this._initState()
}
// ------------------------------------
// Universal
// ------------------------------------
setUniversal (key, value, isJson) {
// Local state
this.setState(key, value)
// Cookies
this.setCookie(key, value)
// Local Storage
this.setLocalStorage(key, value, isJson)
return value
}
getUniversal (key, isJson) {
// Local state
let value = this.getState(key)
// Cookies
if (isUnset(value)) {
value = this.getCookie(key, isJson)
}
// Local Storage
if (isUnset(value)) {
value = this.getLocalStorage(key, isJson)
}
return value
}
syncUniversal (key, defaultValue, isJson) {
let value = this.getUniversal(key, isJson)
if (isUnset(value) && isSet(defaultValue)) {
value = defaultValue
}
if (isSet(value)) {
this.setUniversal(key, value)
}
return value
}
// ------------------------------------
// Local state (reactive)
// ------------------------------------
_initState () {
// Private state is suitable to keep information not being exposed to Vuex store
// This helps prevent stealing token from SSR response HTML
Vue.set(this, '_state', {})
// Use vuex for local state's if possible
this._useVuex = this.options.vuex && this.ctx.store
if (this._useVuex) {
const storeModule = {
namespaced: true,
state: () => this.options.initialState,
mutations: {
SET (state, payload) {
Vue.set(state, payload.key, payload.value)
}
}
}
this.ctx.store.registerModule(this.options.vuex.namespace, storeModule, {
preserveState: Boolean(this.ctx.store.state[this.options.vuex.namespace])
})
this.state = this.ctx.store.state[this.options.vuex.namespace]
} else {
Vue.set(this, 'state', {})
}
}
setState (key, value) {
if (key[0] === '_') {
Vue.set(this._state, key, value)
} else {
if (this._useVuex) {
this.ctx.store.commit(this.options.vuex.namespace + '/SET', {
key,
value
})
} else {
Vue.set(this.state, key, value)
}
}
return value
}
getState (key) {
if (key[0] !== '_') {
return this.state[key]
} else {
return this._state[key]
}
}
watchState (key, fn) {
if (this._useVuex) {
return this.ctx.store.watch(
state => getProp(state[this.options.vuex.namespace], key),
fn
)
}
}
// ------------------------------------
// Local storage
// ------------------------------------
setLocalStorage (key, value, isJson) {
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)
}
return value
}
getLocalStorage (key, isJson) {
if (typeof localStorage === 'undefined' || !this.options.localStorage) {
return
}
const _key = this.options.localStorage.prefix + key
const value = localStorage.getItem(_key)
if (value === 'false') {
return false
}
return isJson ? JSON.parse(value) : value
}
// ------------------------------------
// Cookies
// ------------------------------------
setCookie (key, value, options = {}) {
if (process.server || !this.options.cookie) {
return
}
const _key = this.options.cookie.prefix + key
const _options = Object.assign({}, this.options.cookie.options, options)
if (isUnset(value)) {
Cookies.remove(_key, _options)
} else {
Cookies.set(_key, value, _options)
}
return value
}
getCookie (key, isJson) {
if (!this.options.cookie || (process.server && !this.ctx.req)) {
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]
if (value === 'false') {
return false
}
return isJson ? JSON.parse(value) : value
}
}