Skip to content

Commit 817fab0

Browse files
Merge pull request #647 from dekundu/master
Added popup related error handling for implicit grant
2 parents 3f02a78 + c0bb15f commit 817fab0

File tree

2 files changed

+37
-13
lines changed

2 files changed

+37
-13
lines changed

projects/lib/src/events.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ export type EventType =
2020
| 'session_changed'
2121
| 'session_error'
2222
| 'session_terminated'
23-
| 'logout';
23+
| 'logout'
24+
| 'popup_closed'
25+
| 'popup_blocked';
2426

2527
export abstract class OAuthEvent {
2628
constructor(readonly type: EventType) {}

projects/lib/src/oauth-service.ts

+34-12
Original file line numberDiff line numberDiff line change
@@ -927,27 +927,49 @@ export class OAuthService extends AuthConfig implements OnDestroy {
927927
display: 'popup'
928928
}).then(url => {
929929
return new Promise((resolve, reject) => {
930+
/**
931+
* Error handling section
932+
*/
933+
const checkForPopupClosedInterval = 500;
930934
let windowRef = window.open(url, '_blank', this.calculatePopupFeatures(options));
935+
let checkForPopupClosedTimer: any;
936+
const checkForPopupClosed = () => {
937+
if (!windowRef || windowRef.closed) {
938+
cleanup();
939+
reject(new OAuthErrorEvent('popup_closed', {}));
940+
}
941+
};
942+
if (!windowRef) {
943+
reject(new OAuthErrorEvent('popup_blocked', {}));
944+
} else {
945+
checkForPopupClosedTimer = window.setInterval(checkForPopupClosed, checkForPopupClosedInterval);
946+
}
931947

932948
const cleanup = () => {
949+
window.clearInterval(checkForPopupClosedTimer);
933950
window.removeEventListener('message', listener);
934-
windowRef.close();
951+
if (windowRef !== null) {
952+
windowRef.close();
953+
}
935954
windowRef = null;
936955
};
937956

938957
const listener = (e: MessageEvent) => {
939958
const message = this.processMessageEventMessage(e);
940-
941-
this.tryLogin({
942-
customHashFragment: message,
943-
preventClearHashAfterLogin: true,
944-
}).then(() => {
945-
cleanup();
946-
resolve();
947-
}, err => {
948-
cleanup();
949-
reject(err);
950-
});
959+
if (message && message !== null) {
960+
this.tryLogin({
961+
customHashFragment: message,
962+
preventClearHashAfterLogin: true,
963+
}).then(() => {
964+
cleanup();
965+
resolve();
966+
}, err => {
967+
cleanup();
968+
reject(err);
969+
});
970+
} else {
971+
console.log('false event firing');
972+
}
951973
};
952974

953975
window.addEventListener('message', listener);

0 commit comments

Comments
 (0)