Skip to content
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

#1134 Add possibility to automatically close popup window after som… #1146

Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions docs/oidc-client-ts.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,7 @@ export interface OidcStandardClaims {
export interface PopupWindowFeatures {
// (undocumented)
[k: string]: boolean | string | number | undefined;
closePopupWindowAfter?: number;
// (undocumented)
height?: number;
// (undocumented)
Expand Down
13 changes: 13 additions & 0 deletions src/UserManagerSettings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,19 @@ describe("UserManagerSettings", () => {
expect(subject.popupWindowFeatures).toEqual({ status: true });
});

it("should set closePopupWindowAfter", () => {
// act
const closePopupWindowAfter = 100;
const subject = new UserManagerSettingsStore({
authority: "authority",
client_id: "client",
redirect_uri: "redirect",
popupWindowFeatures: { status: true, closePopupWindowAfter },
});

// assert
expect(subject.popupWindowFeatures).toEqual({ status: true, closePopupWindowAfter });
});
});

describe("popupWindowTarget", () => {
Expand Down
3 changes: 2 additions & 1 deletion src/UserManagerSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const DefaultPopupWindowFeatures: PopupWindowFeatures = {
location: false,
toolbar: false,
height: 640,
closePopupWindowAfter: 0,
pamapa marked this conversation as resolved.
Show resolved Hide resolved
};
export const DefaultPopupTarget = "_blank";
const DefaultAccessTokenExpiringNotificationTimeInSeconds = 60;
Expand All @@ -28,7 +29,7 @@ export interface UserManagerSettings extends OidcClientSettings {
/**
* The features parameter to window.open for the popup signin window. By default, the popup is
* placed centered in front of the window opener.
* (default: \{ location: false, menubar: false, height: 640 \})
* (default: \{ location: false, menubar: false, height: 640, closePopupWindowAfter: 0 \})
*/
popupWindowFeatures?: PopupWindowFeatures;
/** The target parameter to window.open for the popup signin window (default: "_blank") */
Expand Down
28 changes: 28 additions & 0 deletions src/navigators/PopupWindow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,32 @@ describe("PopupWindow", () => {
keepOpen: false,
}, window.location.origin);
});

it("should close the window after closePopupWindowAfter is greater than 0", async () => {
const popupWindow = new PopupWindow({ popupWindowFeatures: { closePopupWindowAfter: 1 } });

const promise = popupWindow.navigate({ url: "http://sts/authorize?x=y", state: "someid" });

jest.runOnlyPendingTimers();
await expect(promise).rejects.toThrow("Popup blocked by user");
jest.runAllTimers();
});

it("shouldnt close the window after closePopupWindowAfter is equal to 0", async () => {
jest.spyOn(global, "setTimeout");

new PopupWindow({ popupWindowFeatures: { closePopupWindowAfter: 0 } });

jest.runOnlyPendingTimers();
expect(setTimeout).toHaveBeenCalledTimes(0);
});

it("shouldnt close the window after closePopupWindowAfter is less than 0", async () => {
jest.spyOn(global, "setTimeout");

new PopupWindow({ popupWindowFeatures: { closePopupWindowAfter: -120 } });

jest.runOnlyPendingTimers();
expect(setTimeout).toHaveBeenCalledTimes(0);
});
});
7 changes: 7 additions & 0 deletions src/navigators/PopupWindow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ export class PopupWindow extends AbstractChildWindow {
super();
const centeredPopup = PopupUtils.center({ ...DefaultPopupWindowFeatures, ...popupWindowFeatures });
this._window = window.open(undefined, popupWindowTarget, PopupUtils.serialize(centeredPopup));
if (popupWindowFeatures?.closePopupWindowAfter && popupWindowFeatures?.closePopupWindowAfter > 0) {
pamapa marked this conversation as resolved.
Show resolved Hide resolved
setTimeout(() => {
if (!this._window || typeof this._window.closed !== "boolean" || this._window.closed) {
this._abort.raise(new Error("Popup blocked by user"));
}
}, popupWindowFeatures.closePopupWindowAfter);
}
}

public async navigate(params: NavigateParams): Promise<NavigateResponse> {
Expand Down
2 changes: 2 additions & 0 deletions src/utils/PopupUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export interface PopupWindowFeatures {
status?: boolean | string;
resizable?: boolean | string;
scrollbars?: boolean | string;
/** Close popup window after time in milliseconds, by default it is 0. To enable this feature set value greater than 0 */
closePopupWindowAfter?: number;

[k: string]: boolean | string | number | undefined;
}
Expand Down