Skip to content

Add check for localStorage writeability #1105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 12, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions projects/lib/src/oauth-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -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
Expand Down