From a159bc811e3514bb2a5b300140e8e28faaa20e9f Mon Sep 17 00:00:00 2001 From: Dennis Holmer Date: Wed, 28 Jul 2021 15:46:08 +0000 Subject: [PATCH] Add check for localStorage writeability --- projects/lib/src/oauth-service.ts | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/projects/lib/src/oauth-service.ts b/projects/lib/src/oauth-service.ts index 9fe14465..7de75330 100644 --- a/projects/lib/src/oauth-service.ts +++ b/projects/lib/src/oauth-service.ts @@ -162,10 +162,7 @@ export class OAuthService extends AuthConfig implements OnDestroy { } // in IE, sessionStorage does not always survive a redirect - if ( - typeof window !== 'undefined' && - typeof window['localStorage'] !== 'undefined' - ) { + if (this.checkLocalStorageAccessable()) { const ua = window?.navigator?.userAgent; const msie = ua?.includes('MSIE ') || ua?.includes('Trident'); @@ -177,6 +174,23 @@ export class OAuthService extends AuthConfig implements OnDestroy { this.setupRefreshTimer(); } + private checkLocalStorageAccessable(){ + if(typeof window === 'undefined') + return false; + + const test = 'test'; + try { + if(typeof window['localStorage'] === 'undefined') + return false; + + localStorage.setItem(test, test); + localStorage.removeItem(test); + return true; + } catch(e) { + return false; + } + } + /** * Use this method to configure the service * @param config the configuration