Skip to content

Commit 8cb450e

Browse files
author
Nikolay Dolzhenkov
committed
edited the docs and sample app to support the MR
1 parent 8797d01 commit 8cb450e

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

docs-src/silent-refresh.md

+13-2
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ This simple implementation within silent-refresh.html is sufficient in most case
6767
var checks = [/[\?|&|#]code=/, /[\?|&|#]error=/, /[\?|&|#]token=/, /[\?|&|#]id_token=/];
6868
6969
function isResponse(str) {
70-
var count = 0;
7170
if (!str) return false;
7271
for(var i=0; i<checks.length; i++) {
7372
if (str.match(checks[i])) return true;
@@ -77,12 +76,24 @@ This simple implementation within silent-refresh.html is sufficient in most case
7776
7877
var message = isResponse(location.hash) ? location.hash : '#' + location.search;
7978
80-
(window.opener || window.parent).postMessage(message, location.origin);
79+
if (window.parent && window.parent !== window) {
80+
// if loaded as an iframe during silent refresh
81+
window.parent.postMessage(message, location.origin);
82+
} else if (window.opener && window.opener !== window) {
83+
// if loaded as a popup during initial login
84+
window.opener.postMessage(message, location.origin);
85+
} else {
86+
// last resort for a popup which has been through redirects and can't use window.opener
87+
localStorage.setItem('auth_hash', message);
88+
localStorage.removeItem('auth_hash');
89+
}
8190
</script>
8291
</body>
8392
</html>
8493
```
94+
The above example checks if the message in the URL (either hash or query string) is indeed a message returned with a response from an authentication provider and not an arbitrary value and then attempts to forward this message to a parent widow either by `.parent` (when this html is loaded in an iframe as a result of silent refresh) or by `.opener` (when the html is loaded into a popup during initial login) or finally using a storage event (as a fallback for complex cases, e.g. initial login in a popup with a cross-domain auth provider).
8595

96+
8697
Please make sure that this file is copied to your output directory by your build task. When using the CLI you can define it as an asset for this. For this, you have to add the following line to the file ``.angular-cli.json``:
8798

8899
```JSON

projects/sample/src/silent-refresh.html

+12-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
];
1010

1111
function isResponse(str) {
12-
var count = 0;
1312
if (!str) return false;
1413
for (var i = 0; i < checks.length; i++) {
1514
if (str.match(checks[i])) return true;
@@ -21,7 +20,18 @@
2120
? location.hash
2221
: '#' + location.search;
2322

24-
(window.opener || window.parent).postMessage(message, location.origin);
23+
if (window.parent && window.parent !== window) {
24+
// if loaded as an iframe during silent refresh
25+
window.parent.postMessage(message, location.origin);
26+
} else if (window.opener && window.opener !== window) {
27+
// if loaded as a popup during initial login
28+
window.opener.postMessage(message, location.origin);
29+
} else {
30+
// last resort for a popup which has been through redirects and can't use window.opener
31+
localStorage.setItem('auth_hash', message);
32+
localStorage.removeItem('auth_hash');
33+
}
34+
2535
</script>
2636
</body>
2737
</html>

0 commit comments

Comments
 (0)