Skip to content

Commit da3bc80

Browse files
Clear hash in cross-browser consistent way
Fixes manfredsteyer#408
1 parent b4c8731 commit da3bc80

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

projects/lib/src/oauth-service.ts

+15-3
Original file line numberDiff line numberDiff line change
@@ -1239,6 +1239,18 @@ export class OAuthService extends AuthConfig {
12391239
}
12401240
}
12411241

1242+
private clearHash() {
1243+
// By @AndyE at https://stackoverflow.com/a/5298684/419956
1244+
if ('pushState' in history) {
1245+
// Clears the hash *including* the hash sigh, cross-browser.
1246+
history.pushState('', document.title, location.pathname + location.search);
1247+
} else {
1248+
// Fallback for older browsers, might leave the '#' character in the address
1249+
// bar in some cases.
1250+
location.hash = '';
1251+
}
1252+
}
1253+
12421254
/**
12431255
* Checks whether there are tokens in the hash fragment
12441256
* as a result of the implicit flow. These tokens are
@@ -1333,7 +1345,7 @@ export class OAuthService extends AuthConfig {
13331345
if (!this.oidc) {
13341346
this.eventsSubject.next(new OAuthSuccessEvent('token_received'));
13351347
if (this.clearHashAfterLogin && !options.preventClearHashAfterLogin) {
1336-
location.hash = '';
1348+
this.clearHash();
13371349
}
13381350
return Promise.resolve();
13391351
}
@@ -1356,7 +1368,7 @@ export class OAuthService extends AuthConfig {
13561368
this.storeIdToken(result);
13571369
this.storeSessionState(sessionState);
13581370
if (this.clearHashAfterLogin) {
1359-
location.hash = '';
1371+
this.clearHash();
13601372
}
13611373
this.eventsSubject.next(new OAuthSuccessEvent('token_received'));
13621374
this.callOnTokenReceivedIfExists(options);
@@ -1405,7 +1417,7 @@ export class OAuthService extends AuthConfig {
14051417
options.onLoginError(parts);
14061418
}
14071419
if (this.clearHashAfterLogin) {
1408-
location.hash = '';
1420+
this.clearHash();
14091421
}
14101422
}
14111423

0 commit comments

Comments
 (0)