@@ -2,7 +2,7 @@ import Cookies from 'js-cookie'
2
2
import { parse as parseCookie } from 'cookie'
3
3
import getProp from 'dotprop'
4
4
import Vue from 'vue'
5
- import { routeOption , isRelativeURL , isUnset , isSameURL } from './utilities'
5
+ import { routeOption , isRelativeURL , isUnset , isSet , isSameURL } from './utilities'
6
6
7
7
export default class Auth {
8
8
constructor ( ctx , options ) {
@@ -24,21 +24,16 @@ export default class Auth {
24
24
// Register vuex store
25
25
this . _registerVuexStore ( )
26
26
27
- // Reset on error
28
- if ( this . options . resetOnError ) {
29
- this . _resetOnError ( )
30
- }
31
-
32
27
// Watch for loggedIn changes only in client side
33
28
if ( process . browser ) {
34
29
this . _autoRedirect ( )
35
30
}
36
31
37
- // Sync token
38
- this . syncToken ( )
32
+ // Restore strategy
33
+ this . syncUniversal ( 'strategy' , this . options . defaultStrategy )
39
34
40
- // Set defaultStrategy
41
- return this . setStrategy ( )
35
+ // Call mounted for active strategy on initial load
36
+ return this . mounted ( )
42
37
}
43
38
44
39
// ---------------------------------------------------------------
@@ -65,12 +60,6 @@ export default class Auth {
65
60
} )
66
61
}
67
62
68
- _resetOnError ( ) {
69
- this . onError ( ( ) => {
70
- this . reset ( )
71
- } )
72
- }
73
-
74
63
_autoRedirect ( ) {
75
64
this . watchState ( 'loggedIn' , loggedIn => {
76
65
if ( ! routeOption ( this . ctx . route , 'auth' , false ) ) {
@@ -85,38 +74,42 @@ export default class Auth {
85
74
86
75
// ...Universal
87
76
88
- setUniversal ( key , value ) {
77
+ setUniversal ( key , value , isJson ) {
89
78
// Local state
90
79
this . setState ( key , value )
91
80
92
81
// Cookies
93
82
this . setCookie ( key , value )
94
83
95
84
// Local Storage
96
- this . setLocalStorage ( key , value )
85
+ this . setLocalStorage ( key , value , isJson )
97
86
}
98
87
99
- getUniversal ( key ) {
88
+ getUniversal ( key , isJson ) {
100
89
// Local state
101
90
let value = this . getState ( key )
102
91
103
92
// Cookies
104
93
if ( isUnset ( value ) ) {
105
- value = this . getCookie ( key )
94
+ value = this . getCookie ( key , isJson )
106
95
}
107
96
108
97
// Local Storage
109
98
if ( isUnset ( value ) ) {
110
- value = this . getLocalStorage ( key )
99
+ value = this . getLocalStorage ( key , isJson )
111
100
}
112
101
113
102
return value
114
103
}
115
104
116
- syncUniversal ( key ) {
117
- const value = this . getUniversal ( key )
105
+ syncUniversal ( key , defaultValue , isJson ) {
106
+ let value = this . getUniversal ( key , isJson )
118
107
119
- if ( ! isUnset ( value ) ) {
108
+ if ( isUnset ( value ) && isSet ( defaultValue ) ) {
109
+ value = defaultValue
110
+ }
111
+
112
+ if ( isSet ( value ) ) {
120
113
this . setUniversal ( key , value )
121
114
}
122
115
@@ -154,19 +147,20 @@ export default class Auth {
154
147
155
148
// ...Local Storage
156
149
157
- setLocalStorage ( key , value ) {
150
+ setLocalStorage ( key , value , isJson ) {
158
151
if ( typeof localStorage !== 'undefined' ) {
159
152
if ( isUnset ( value ) ) {
160
153
localStorage . removeItem ( key )
161
154
} else {
162
- localStorage . setItem ( key , value )
155
+ localStorage . setItem ( key , isJson ? JSON . stringify ( value ) : value )
163
156
}
164
157
}
165
158
}
166
159
167
- getLocalStorage ( key ) {
160
+ getLocalStorage ( key , isJson ) {
168
161
if ( typeof localStorage !== 'undefined' ) {
169
- return localStorage . getItem ( key )
162
+ const value = localStorage . getItem ( key )
163
+ return isJson ? JSON . parse ( value ) : value
170
164
}
171
165
}
172
166
@@ -186,7 +180,7 @@ export default class Auth {
186
180
}
187
181
}
188
182
189
- getCookie ( key ) {
183
+ getCookie ( key , isJson ) {
190
184
if ( ! this . options . cookie ) {
191
185
return
192
186
}
@@ -196,8 +190,9 @@ export default class Auth {
196
190
: this . ctx . req . headers . cookie
197
191
198
192
const cookies = parseCookie ( cookieStr || '' ) || { }
193
+ const value = cookies [ key ]
199
194
200
- return cookies [ key ]
195
+ return isJson ? JSON . parse ( value ) : value
201
196
}
202
197
203
198
// ---------------------------------------------------------------
@@ -213,41 +208,74 @@ export default class Auth {
213
208
}
214
209
215
210
setStrategy ( name ) {
216
- if ( name === undefined ) {
217
- name = this . options . defaultStrategy
218
- }
219
-
220
- const oldName = this . getState ( 'strategy' )
221
-
222
- if ( oldName === name ) {
211
+ if ( name === this . getUniversal ( 'strategy' ) ) {
223
212
return Promise . resolve ( )
224
213
}
225
214
226
- // Update state
227
- this . setState ( 'strategy' , name )
215
+ // Call to reset
216
+ this . reset ( )
217
+
218
+ // Set strategy
219
+ this . setUniversal ( 'strategy' , name )
228
220
229
221
// Call mounted hook on active strategy
230
- return this . _mounted ( )
222
+ return this . mounted ( )
231
223
}
232
224
233
225
// ---------------------------------------------------------------
234
- // Scheme interface wrappers
226
+ // Scheme interface wrappers and default handlers
235
227
// ---------------------------------------------------------------
236
228
237
- _mounted ( ) {
238
- return this . strategy . mounted ( ...arguments )
229
+ mounted ( ) {
230
+ if ( this . strategy . mounted ) {
231
+ return Promise . resolve ( this . strategy . mounted ( ...arguments ) ) . then ( ( ) => this . fetchUserOnce ( ) )
232
+ }
233
+
234
+ return this . fetchUserOnce ( )
239
235
}
240
236
241
237
login ( ) {
242
- return this . strategy . login ( ...arguments )
238
+ if ( this . strategy . login ) {
239
+ return Promise . resolve ( this . strategy . login ( ...arguments ) )
240
+ }
241
+
242
+ return Promise . resolve ( )
243
243
}
244
244
245
245
fetchUser ( ) {
246
- return this . strategy . fetchUser ( ...arguments )
246
+ if ( this . strategy . fetchUser ) {
247
+ return Promise . resolve ( this . strategy . fetchUser ( ...arguments ) )
248
+ }
249
+
250
+ return Promise . resolve ( )
247
251
}
248
252
249
253
logout ( ) {
250
- return this . strategy . logout ( ...arguments )
254
+ if ( this . strategy . logout ) {
255
+ return Promise . resolve ( this . strategy . logout ( ...arguments ) ) . then ( ( ) => this . reset ( ) )
256
+ }
257
+
258
+ this . reset ( )
259
+
260
+ return Promise . resolve ( )
261
+ }
262
+
263
+ fetchUserOnce ( ) {
264
+ if ( ! this . state . user ) {
265
+ return this . fetchUser ( ...arguments )
266
+ }
267
+
268
+ return Promise . resolve ( )
269
+ }
270
+
271
+ reset ( ) {
272
+ if ( this . strategy . reset ) {
273
+ this . strategy . reset ( ...arguments )
274
+ }
275
+
276
+ this . setState ( 'loggedIn' , false )
277
+ this . setState ( 'user' , null )
278
+ this . setToken ( null )
251
279
}
252
280
253
281
// ---------------------------------------------------------------
@@ -329,12 +357,6 @@ export default class Auth {
329
357
}
330
358
}
331
359
332
- reset ( ) {
333
- this . setState ( 'loggedIn' , false )
334
- this . setState ( 'user' , null )
335
- this . setToken ( null )
336
- }
337
-
338
360
redirect ( name ) {
339
361
if ( ! this . options . redirect ) {
340
362
return
0 commit comments