@@ -189,11 +189,11 @@ export class Storage {
189
189
return this . removeLocalStorage ( key )
190
190
}
191
191
192
- if ( ! this . localStorageEnabled ( ) || ! this . options . localStorage ) {
192
+ if ( ! this . isLocalStorageEnabled ( ) ) {
193
193
return
194
194
}
195
195
196
- const _key = this . options . localStorage . prefix + key
196
+ const _key = this . getPrefix ( ) + key
197
197
198
198
try {
199
199
localStorage . setItem ( _key , encodeValue ( value ) )
@@ -207,23 +207,24 @@ export class Storage {
207
207
}
208
208
209
209
getLocalStorage ( key : string ) : unknown {
210
- if ( ! this . localStorageEnabled ( ) || ! this . options . localStorage ) {
210
+ if ( ! this . isLocalStorageEnabled ( ) ) {
211
211
return
212
212
}
213
213
214
- const _key = this . options . localStorage . prefix + key
214
+ const _key = this . getPrefix ( ) + key
215
215
216
216
const value = localStorage . getItem ( _key )
217
217
218
218
return decodeValue ( value )
219
219
}
220
220
221
221
removeLocalStorage ( key : string ) : void {
222
- if ( ! this . localStorageEnabled ( ) || ! this . options . localStorage ) {
222
+ if ( ! this . isLocalStorageEnabled ( ) ) {
223
223
return
224
224
}
225
225
226
- const _key = this . options . localStorage . prefix + key
226
+ const _key = this . getPrefix ( ) + key
227
+
227
228
localStorage . removeItem ( _key )
228
229
}
229
230
@@ -307,7 +308,24 @@ export class Storage {
307
308
this . setCookie ( key , undefined , options )
308
309
}
309
310
310
- localStorageEnabled ( ) : boolean {
311
+ getPrefix ( ) : string {
312
+ if ( ! this . options . localStorage ) {
313
+ throw new Error ( 'Cannot get prefix; localStorage is off' )
314
+ }
315
+ return this . options . localStorage . prefix
316
+ }
317
+
318
+ isLocalStorageEnabled ( ) : boolean {
319
+ // Disabled by configuration
320
+ if ( ! this . options . localStorage ) {
321
+ return false
322
+ }
323
+
324
+ // Local Storage only exists in the browser
325
+ if ( ! process . client ) {
326
+ return false
327
+ }
328
+
311
329
// There's no great way to check if localStorage is enabled; most solutions
312
330
// error out. So have to use this hacky approach :\
313
331
// https://stackoverflow.com/questions/16427636/check-if-localstorage-is-available
@@ -317,10 +335,11 @@ export class Storage {
317
335
localStorage . removeItem ( test )
318
336
return true
319
337
} catch ( e ) {
320
- if ( this . options . localStorage ) {
338
+ if ( ! this . options . ignoreExceptions ) {
321
339
// eslint-disable-next-line no-console
322
340
console . warn (
323
- "[AUTH] Local storage is enabled in config, but browser doesn't support it"
341
+ "[AUTH] Local storage is enabled in config, but browser doesn't" +
342
+ ' support it'
324
343
)
325
344
}
326
345
return false
0 commit comments