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

[AUT-1539] - Child Window Pop up option #46

Merged
merged 5 commits into from
Oct 29, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
cleanup
  • Loading branch information
hwhmeikle committed Oct 26, 2023
commit ccea6b19d7d38363f887245f484e6c90ef85b259
123 changes: 71 additions & 52 deletions src/authsignal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import {
AuthsignalWindowMessage,
AuthsignalWindowMessageData,
LaunchOptions,
PopupLaunchOptions,
TokenPayload,
WindowLaunchOptions,
} from "./types";
import {PopupHandler} from "./popup-handler";
import {PopupHandler} from "./handlers/popup-handler";
import {Passkey} from "./passkey";
import {WindowHandler} from "./window-handler";
import {WindowHandler} from "./handlers/window-handler";

const DEFAULT_COOKIE_NAME = "__as_aid";

Expand Down Expand Up @@ -60,65 +62,82 @@ export class Authsignal {
launch(url: string, options?: {mode: "popup"} & LaunchOptions): Promise<TokenPayload>;
launch(url: string, options?: {mode: "window"} & LaunchOptions): Promise<TokenPayload>;
launch(url: string, options?: LaunchOptions) {
if (!options?.mode || options.mode === "redirect") {
window.location.href = url;
} else if (!options?.mode || options.mode === "window") {
const {popupOptions} = options;

const Window = new WindowHandler();
const popupUrl = `${url}&mode=popup`;
Window.show({url: popupUrl, width: popupOptions?.width, height: popupOptions?.height});
return new Promise<TokenPayload>((resolve) => {
const onMessage = (event: MessageEvent) => {
let data: AuthsignalWindowMessageData | null = null;

try {
data = JSON.parse(event.data) as AuthsignalWindowMessageData;
} catch {
// Ignore if the event data is not valid JSON
}

if (data?.event === AuthsignalWindowMessage.AUTHSIGNAL_CLOSE_POPUP) {
this._token = data.token;

Window.close();
resolve({token: this._token});
}
};
window.addEventListener("message", onMessage, false);
});
} else {
const {popupOptions} = options;
switch (options?.mode) {
case "window":
return this.launchWithWindow(url, options);
case "popup":
return this.launchWithPopup(url, options);
case "redirect":
default:
this.launchWithRedirect(url);
}
}

private launchWithRedirect(url: string) {
window.location.href = url;
}

const Popup = new PopupHandler({width: popupOptions?.width});
private launchWithPopup(url: string, options: PopupLaunchOptions) {
const {popupOptions} = options;

const popupUrl = `${url}&mode=popup`;
const Popup = new PopupHandler({width: popupOptions?.width});

Popup.show({url: popupUrl});
const popupUrl = `${url}&mode=popup`;

return new Promise<TokenPayload>((resolve) => {
const onMessage = (event: MessageEvent) => {
let data: AuthsignalWindowMessageData | null = null;
Popup.show({url: popupUrl});

try {
data = JSON.parse(event.data) as AuthsignalWindowMessageData;
} catch {
// Ignore if the event data is not valid JSON
}
return new Promise<TokenPayload>((resolve) => {
const onMessage = (event: MessageEvent) => {
let data: AuthsignalWindowMessageData | null = null;

if (data?.event === AuthsignalWindowMessage.AUTHSIGNAL_CLOSE_POPUP) {
this._token = data.token;
try {
data = JSON.parse(event.data) as AuthsignalWindowMessageData;
} catch {
// Ignore if the event data is not valid JSON
}

Popup.close();
}
};
if (data?.event === AuthsignalWindowMessage.AUTHSIGNAL_CLOSE_POPUP) {
this._token = data.token;

Popup.on("hide", () => {
resolve({token: this._token});
});
Popup.close();
}
};

window.addEventListener("message", onMessage, false);
Popup.on("hide", () => {
resolve({token: this._token});
});
}

window.addEventListener("message", onMessage, false);
});
}

private launchWithWindow(url: string, options: WindowLaunchOptions) {
const {windowOptions} = options;

const Window = new WindowHandler();

const popupUrl = `${url}&mode=popup`;

Window.show({url: popupUrl, width: windowOptions?.width, height: windowOptions?.height});

return new Promise<TokenPayload>((resolve) => {
const onMessage = (event: MessageEvent) => {
let data: AuthsignalWindowMessageData | null = null;

try {
data = JSON.parse(event.data) as AuthsignalWindowMessageData;
} catch {
// Ignore if the event data is not valid JSON
}

if (data?.event === AuthsignalWindowMessage.AUTHSIGNAL_CLOSE_POPUP) {
this._token = data.token;

Window.close();
resolve({token: this._token});
}
};
window.addEventListener("message", onMessage, false);
});
}
}
2 changes: 2 additions & 0 deletions src/handlers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export {WindowHandler} from "./window-handler";
export {PopupHandler} from "./popup-handler";
4 changes: 1 addition & 3 deletions src/popup-handler.ts → src/handlers/popup-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type PopupHandlerOptions = {
width?: string;
};

class PopupHandler {
export class PopupHandler {
private popup: A11yDialog | null = null;

constructor({width}: PopupHandlerOptions) {
Expand Down Expand Up @@ -174,5 +174,3 @@ function resizeIframe(event: MessageEvent) {
iframeEl.style.height = event.data.height + "px";
}
}

export {PopupHandler};
54 changes: 54 additions & 0 deletions src/handlers/window-handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
type PopupShowInput = {
url: string;
width?: number;
height?: number;
};

const DEFAULT_WIDTH = 400;
const DEFAULT_HEIGHT = 500;

export class WindowHandler {
private windowRef: Window | null = null;

show({url, width = DEFAULT_WIDTH, height = DEFAULT_HEIGHT}: PopupShowInput) {
const windowRef = openWindow({url, width, height, win: window});

if (!windowRef) {
throw new Error("Window is not initialized");
}

this.windowRef = windowRef;

return windowRef;
}

close() {
if (!this.windowRef) {
throw new Error("Window is not initialized");
}

this.windowRef.close();
}
}

type WindowPopupCenterOptions = {
url: string;
width: number;
height: number;
win: Window;
};

function openWindow({url, width, height, win}: WindowPopupCenterOptions): Window | null {
if (!win.top) {
return null;
}

const y = win.top.outerHeight / 2 + win.top.screenY - height / 2;
const x = win.top.outerWidth / 2 + win.top.screenX - width / 2;

return window.open(
url,
"",
`toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=${width}, height=${height}, top=${y}, left=${x}`
);
}
24 changes: 0 additions & 24 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,6 @@ type CookieOptions = {
secure: boolean;
};

type WindowPopupCenterOptions = {
url: string;
width: string;
height: string;
win: Window;
};

export const popupCenterScreen = ({url, width, height, win}: WindowPopupCenterOptions): Window | null => {
if (!win.top) {
return null;
}

const w = parseInt(width);
const h = parseInt(height);

const y = win.top.outerHeight / 2 + win.top.screenY - h / 2;
const x = win.top.outerWidth / 2 + win.top.screenX - w / 2;
return window.open(
url,
"",
`toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=${w}, height=${h}, top=${y}, left=${x}`
);
};

export const setCookie = ({name, value, expire, domain, secure}: CookieOptions): void => {
const expireString = expire === Infinity ? " expires=Fri, 31 Dec 9999 23:59:59 GMT" : "; max-age=" + expire;
document.cookie =
Expand Down
16 changes: 6 additions & 10 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ type BaseLaunchOptions = {
mode?: "popup" | "redirect" | "window";
};

type RedirectLaunchOptions = BaseLaunchOptions & {
export type RedirectLaunchOptions = BaseLaunchOptions & {
mode: "redirect";
};

type PopupLaunchOptions = BaseLaunchOptions & {
export type PopupLaunchOptions = BaseLaunchOptions & {
mode: "popup";
popupOptions?: {
/** Any valid CSS value for the `width` property. */
Expand All @@ -24,15 +24,11 @@ type PopupLaunchOptions = BaseLaunchOptions & {
};
};

type WindowLaunchOptions = BaseLaunchOptions & {
export type WindowLaunchOptions = BaseLaunchOptions & {
mode: "window";
popupOptions?: {
/** Any valid CSS value for the `width` property. */
width?: string;
/**
* @deprecated The popup will automatically resize to fit the content.
*/
height?: string;
windowOptions?: {
width?: number;
height?: number;
};
};

Expand Down
34 changes: 0 additions & 34 deletions src/window-handler.ts

This file was deleted.