@@ -2,7 +2,7 @@ import Cookies from 'js-cookie'
22import { parse as parseCookie } from 'cookie'
33import getProp from 'dotprop'
44import Vue from 'vue'
5- import { routeOption , isRelativeURL , isUnset , isSameURL } from './utilities'
5+ import { routeOption , isRelativeURL , isUnset , isSet , isSameURL } from './utilities'
66
77export default class Auth {
88 constructor ( ctx , options ) {
@@ -24,21 +24,16 @@ export default class Auth {
2424 // Register vuex store
2525 this . _registerVuexStore ( )
2626
27- // Reset on error
28- if ( this . options . resetOnError ) {
29- this . _resetOnError ( )
30- }
31-
3227 // Watch for loggedIn changes only in client side
3328 if ( process . browser ) {
3429 this . _autoRedirect ( )
3530 }
3631
37- // Sync token
38- this . syncToken ( )
32+ // Restore strategy
33+ this . syncUniversal ( 'strategy' , this . options . defaultStrategy )
3934
40- // Set defaultStrategy
41- return this . setStrategy ( )
35+ // Call mounted for active strategy on initial load
36+ return this . mounted ( )
4237 }
4338
4439 // ---------------------------------------------------------------
@@ -65,12 +60,6 @@ export default class Auth {
6560 } )
6661 }
6762
68- _resetOnError ( ) {
69- this . onError ( ( ) => {
70- this . reset ( )
71- } )
72- }
73-
7463 _autoRedirect ( ) {
7564 this . watchState ( 'loggedIn' , loggedIn => {
7665 if ( ! routeOption ( this . ctx . route , 'auth' , false ) ) {
@@ -85,38 +74,42 @@ export default class Auth {
8574
8675 // ...Universal
8776
88- setUniversal ( key , value ) {
77+ setUniversal ( key , value , isJson ) {
8978 // Local state
9079 this . setState ( key , value )
9180
9281 // Cookies
9382 this . setCookie ( key , value )
9483
9584 // Local Storage
96- this . setLocalStorage ( key , value )
85+ this . setLocalStorage ( key , value , isJson )
9786 }
9887
99- getUniversal ( key ) {
88+ getUniversal ( key , isJson ) {
10089 // Local state
10190 let value = this . getState ( key )
10291
10392 // Cookies
10493 if ( isUnset ( value ) ) {
105- value = this . getCookie ( key )
94+ value = this . getCookie ( key , isJson )
10695 }
10796
10897 // Local Storage
10998 if ( isUnset ( value ) ) {
110- value = this . getLocalStorage ( key )
99+ value = this . getLocalStorage ( key , isJson )
111100 }
112101
113102 return value
114103 }
115104
116- syncUniversal ( key ) {
117- const value = this . getUniversal ( key )
105+ syncUniversal ( key , defaultValue , isJson ) {
106+ let value = this . getUniversal ( key , isJson )
118107
119- if ( ! isUnset ( value ) ) {
108+ if ( isUnset ( value ) && isSet ( defaultValue ) ) {
109+ value = defaultValue
110+ }
111+
112+ if ( isSet ( value ) ) {
120113 this . setUniversal ( key , value )
121114 }
122115
@@ -154,19 +147,20 @@ export default class Auth {
154147
155148 // ...Local Storage
156149
157- setLocalStorage ( key , value ) {
150+ setLocalStorage ( key , value , isJson ) {
158151 if ( typeof localStorage !== 'undefined' ) {
159152 if ( isUnset ( value ) ) {
160153 localStorage . removeItem ( key )
161154 } else {
162- localStorage . setItem ( key , value )
155+ localStorage . setItem ( key , isJson ? JSON . stringify ( value ) : value )
163156 }
164157 }
165158 }
166159
167- getLocalStorage ( key ) {
160+ getLocalStorage ( key , isJson ) {
168161 if ( typeof localStorage !== 'undefined' ) {
169- return localStorage . getItem ( key )
162+ const value = localStorage . getItem ( key )
163+ return isJson ? JSON . parse ( value ) : value
170164 }
171165 }
172166
@@ -186,7 +180,7 @@ export default class Auth {
186180 }
187181 }
188182
189- getCookie ( key ) {
183+ getCookie ( key , isJson ) {
190184 if ( ! this . options . cookie ) {
191185 return
192186 }
@@ -196,8 +190,9 @@ export default class Auth {
196190 : this . ctx . req . headers . cookie
197191
198192 const cookies = parseCookie ( cookieStr || '' ) || { }
193+ const value = cookies [ key ]
199194
200- return cookies [ key ]
195+ return isJson ? JSON . parse ( value ) : value
201196 }
202197
203198 // ---------------------------------------------------------------
@@ -213,41 +208,74 @@ export default class Auth {
213208 }
214209
215210 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' ) ) {
223212 return Promise . resolve ( )
224213 }
225214
226- // Update state
227- this . setState ( 'strategy' , name )
215+ // Call to reset
216+ this . reset ( )
217+
218+ // Set strategy
219+ this . setUniversal ( 'strategy' , name )
228220
229221 // Call mounted hook on active strategy
230- return this . _mounted ( )
222+ return this . mounted ( )
231223 }
232224
233225 // ---------------------------------------------------------------
234- // Scheme interface wrappers
226+ // Scheme interface wrappers and default handlers
235227 // ---------------------------------------------------------------
236228
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 ( )
239235 }
240236
241237 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 ( )
243243 }
244244
245245 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 ( )
247251 }
248252
249253 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 )
251279 }
252280
253281 // ---------------------------------------------------------------
@@ -329,12 +357,6 @@ export default class Auth {
329357 }
330358 }
331359
332- reset ( ) {
333- this . setState ( 'loggedIn' , false )
334- this . setState ( 'user' , null )
335- this . setToken ( null )
336- }
337-
338360 redirect ( name ) {
339361 if ( ! this . options . redirect ) {
340362 return
0 commit comments