1- // @flow
2-
3- // #region imports
41import decode from 'jwt-decode' ;
5- import isAfter from 'date-fns/is_after' ;
6- // #endregion
7-
8- // #region flow types
9- export type STORES_TYPES = 'localStorage' | 'sessionStorage' ;
10- export type Storage = STORES_TYPES ;
11- export type TokenKey = string ;
12- export type UserInfoKey = string ;
13- // #endregion
2+ import { isAfter } from 'date-fns' ;
143
154// #region constants
165const TOKEN_KEY = 'token' ;
@@ -44,9 +33,9 @@ export const auth = {
4433 * @returns {string } token value
4534 */
4635 getToken (
47- fromStorage : Storage = APP_PERSIST_STORES_TYPES [ 0 ] ,
36+ fromStorage : STORES_TYPES = APP_PERSIST_STORES_TYPES [ 0 ] ,
4837 tokenKey : TokenKey = TOKEN_KEY ,
49- ) : ? string {
38+ ) : string | null {
5039 // localStorage:
5140 if ( fromStorage === APP_PERSIST_STORES_TYPES [ 0 ] ) {
5241 return ( localStorage && localStorage . getItem ( tokenKey ) ) || null ;
@@ -69,22 +58,24 @@ export const auth = {
6958 */
7059 setToken (
7160 value : string = '' ,
72- toStorage : Storage = APP_PERSIST_STORES_TYPES [ 0 ] ,
61+ toStorage : STORES_TYPES = APP_PERSIST_STORES_TYPES [ 0 ] ,
7362 tokenKey : TokenKey = TOKEN_KEY ,
74- ) : ? string {
63+ ) : void {
7564 if ( ! value || value . length <= 0 ) {
7665 return ;
7766 }
7867 // localStorage:
7968 if ( toStorage === APP_PERSIST_STORES_TYPES [ 0 ] ) {
8069 if ( localStorage ) {
8170 localStorage . setItem ( tokenKey , value ) ;
71+ return ;
8272 }
8373 }
8474 // sessionStorage:
8575 if ( toStorage === APP_PERSIST_STORES_TYPES [ 1 ] ) {
8676 if ( sessionStorage ) {
8777 sessionStorage . setItem ( tokenKey , value ) ;
78+ return ;
8879 }
8980 }
9081 } ,
@@ -111,25 +102,25 @@ export const auth = {
111102 * @returns {bool } is authenticed response
112103 */
113104 isAuthenticated (
114- fromStorage : Storage = APP_PERSIST_STORES_TYPES [ 0 ] ,
105+ fromStorage : STORES_TYPES = APP_PERSIST_STORES_TYPES [ 0 ] ,
115106 tokenKey : TokenKey = TOKEN_KEY ,
116107 ) : boolean {
117108 // localStorage:
118109 if ( fromStorage === APP_PERSIST_STORES_TYPES [ 0 ] ) {
119110 if ( localStorage && localStorage . getItem ( tokenKey ) ) {
120111 return true ;
121- } else {
122- return false ;
123112 }
113+ return false ;
124114 }
115+
125116 // sessionStorage:
126117 if ( fromStorage === APP_PERSIST_STORES_TYPES [ 1 ] ) {
127118 if ( sessionStorage && sessionStorage . getItem ( tokenKey ) ) {
128119 return true ;
129- } else {
130- return false ;
131120 }
121+ return false ;
132122 }
123+
133124 // default:
134125 return false ;
135126 } ,
@@ -141,7 +132,7 @@ export const auth = {
141132 * @returns {bool } success/failure flag
142133 */
143134 clearToken (
144- storage : Storage = APP_PERSIST_STORES_TYPES [ 0 ] ,
135+ storage : STORES_TYPES = APP_PERSIST_STORES_TYPES [ 0 ] ,
145136 tokenKey : TokenKey = TOKEN_KEY ,
146137 ) : boolean {
147138 // localStorage:
@@ -169,7 +160,7 @@ export const auth = {
169160 return new Date ( 0 ) ; // is expired
170161 }
171162
172- const token = decode ( encodedToken ) ;
163+ const token : { exp : number } = decode ( encodedToken ) ;
173164 if ( ! token . exp ) {
174165 return new Date ( 0 ) ; // is expired
175166 }
@@ -203,18 +194,34 @@ export const auth = {
203194 * @returns {string } token value
204195 */
205196 getUserInfo (
206- fromStorage : Storage = APP_PERSIST_STORES_TYPES [ 0 ] ,
197+ fromStorage : STORES_TYPES = APP_PERSIST_STORES_TYPES [ 0 ] ,
207198 userInfoKey : UserInfoKey = USER_INFO ,
208- ) : ? string {
199+ ) : any {
209200 // localStorage:
210201 if ( fromStorage === APP_PERSIST_STORES_TYPES [ 0 ] ) {
211- return ( localStorage && parse ( localStorage . getItem ( userInfoKey ) ) ) || null ;
202+ try {
203+ return (
204+ ( window &&
205+ localStorage &&
206+ parse ( localStorage . getItem ( userInfoKey ) || '' ) ) ||
207+ null
208+ ) ;
209+ } catch ( error ) {
210+ return null ;
211+ }
212212 }
213213 // sessionStorage:
214214 if ( fromStorage === APP_PERSIST_STORES_TYPES [ 1 ] ) {
215- return (
216- ( sessionStorage && parse ( sessionStorage . getItem ( userInfoKey ) ) ) || null
217- ) ;
215+ try {
216+ return (
217+ ( window &&
218+ sessionStorage &&
219+ parse ( sessionStorage . getItem ( userInfoKey ) || '' ) ) ||
220+ null
221+ ) ;
222+ } catch ( error ) {
223+ return null ;
224+ }
218225 }
219226 // default:
220227 return null ;
@@ -230,22 +237,26 @@ export const auth = {
230237 */
231238 setUserInfo (
232239 value : string = '' ,
233- toStorage : Storage = APP_PERSIST_STORES_TYPES [ 0 ] ,
240+ toStorage : STORES_TYPES = APP_PERSIST_STORES_TYPES [ 0 ] ,
234241 userInfoKey : UserInfoKey = USER_INFO ,
235242 ) : any {
236243 if ( ! value || value . length <= 0 ) {
237244 return ;
238245 }
246+
239247 // localStorage:
240248 if ( toStorage === APP_PERSIST_STORES_TYPES [ 0 ] ) {
241249 if ( localStorage ) {
242250 localStorage . setItem ( userInfoKey , stringify ( value ) ) ;
251+ return ;
243252 }
244253 }
254+
245255 // sessionStorage:
246256 if ( toStorage === APP_PERSIST_STORES_TYPES [ 1 ] ) {
247257 if ( sessionStorage ) {
248258 sessionStorage . setItem ( userInfoKey , stringify ( value ) ) ;
259+ return ;
249260 }
250261 }
251262 } ,
@@ -260,10 +271,13 @@ export const auth = {
260271 // localStorage:
261272 if ( localStorage && localStorage [ userInfoKey ] ) {
262273 localStorage . removeItem ( userInfoKey ) ;
274+ return ;
263275 }
276+
264277 // sessionStorage:
265278 if ( sessionStorage && sessionStorage [ userInfoKey ] ) {
266279 sessionStorage . removeItem ( userInfoKey ) ;
280+ return ;
267281 }
268282 } ,
269283
@@ -278,9 +292,12 @@ export const auth = {
278292 clearAllAppStorage ( ) : any {
279293 if ( localStorage ) {
280294 localStorage . clear ( ) ;
295+ return ;
281296 }
297+
282298 if ( sessionStorage ) {
283299 sessionStorage . clear ( ) ;
300+ return ;
284301 }
285302 } ,
286303} ;
0 commit comments