Skip to content

Commit ac931de

Browse files
Merge pull request #1105 from Zwapgrid/master
Add check for localStorage writeability
2 parents 31d67dd + a159bc8 commit ac931de

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

projects/lib/src/oauth-service.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,7 @@ export class OAuthService extends AuthConfig implements OnDestroy {
162162
}
163163

164164
// in IE, sessionStorage does not always survive a redirect
165-
if (
166-
typeof window !== 'undefined' &&
167-
typeof window['localStorage'] !== 'undefined'
168-
) {
165+
if (this.checkLocalStorageAccessable()) {
169166
const ua = window?.navigator?.userAgent;
170167
const msie = ua?.includes('MSIE ') || ua?.includes('Trident');
171168

@@ -177,6 +174,23 @@ export class OAuthService extends AuthConfig implements OnDestroy {
177174
this.setupRefreshTimer();
178175
}
179176

177+
private checkLocalStorageAccessable(){
178+
if(typeof window === 'undefined')
179+
return false;
180+
181+
const test = 'test';
182+
try {
183+
if(typeof window['localStorage'] === 'undefined')
184+
return false;
185+
186+
localStorage.setItem(test, test);
187+
localStorage.removeItem(test);
188+
return true;
189+
} catch(e) {
190+
return false;
191+
}
192+
}
193+
180194
/**
181195
* Use this method to configure the service
182196
* @param config the configuration

0 commit comments

Comments
 (0)