1
+ /**
2
+ * Configuration options for popup window dimensions and position.
3
+ */
1
4
export type PopupOptions = {
5
+ /** Width of the popup window in pixels (default: 500) */
2
6
width ?: number ;
7
+ /** Height of the popup window in pixels (default: 600) */
3
8
height ?: number ;
9
+ /** Left position of the popup window in pixels (default: centered) */
4
10
left ?: number ;
11
+ /** Top position of the popup window in pixels (default: centered) */
5
12
top ?: number ;
6
13
} ;
7
14
15
+ /**
16
+ * Checks if the current window is running inside a popup.
17
+ *
18
+ * @returns True if the current window is a popup (self !== top), false otherwise
19
+ */
8
20
export const openInPopup = ( ) : boolean => {
9
21
return window . self !== window . top ;
10
22
} ;
11
23
24
+ /**
25
+ * Configuration options for navigating to Kinde authentication.
26
+ */
12
27
export type NavigateToKindeOptions = {
28
+ /** The URL to navigate to for authentication */
13
29
url : string ;
30
+ /** Optional popup window configuration */
14
31
popupOptions ?: PopupOptions ;
32
+ /** Optional callback to handle authentication results */
15
33
handleResult ?: ( result : URLSearchParams ) => Promise < void > ;
34
+ /** Force popup window (default: false) */
16
35
forcePopup ?: boolean ;
17
36
} ;
18
37
38
+ /**
39
+ * Navigates to Kinde authentication URL, either in a popup or by redirecting the current page.
40
+ *
41
+ * This function determines whether to open the authentication URL in a popup window
42
+ * or redirect the current page based on the current context and options provided.
43
+ * If the current window is in an iFrame a popup or forcePopup is true, it will create
44
+ * a new popup window. Otherwise, it redirects the current page.
45
+ *
46
+ * @param options - Configuration options for the navigation
47
+ * @returns A promise that resolves when navigation is complete
48
+ *
49
+ * @example
50
+ * ```typescript
51
+ * // Navigate to authentication in popup
52
+ * await navigateToKinde({
53
+ * url: "https://app.kinde.com/oauth/authorize?client_id=...",
54
+ * popupOptions: { width: 600, height: 700 },
55
+ * handleResult: async (result) => {
56
+ * console.log("Auth result:", result);
57
+ * // Handle authentication result
58
+ * },
59
+ * forcePopup: true
60
+ * });
61
+ *
62
+ * // Navigate by redirecting current page
63
+ * await navigateToKinde({
64
+ * url: "https://app.kinde.com/oauth/authorize?client_id=...",
65
+ * handleResult: async (result) => {
66
+ * // This will only be called if in popup mode on completion
67
+ * }
68
+ * });
69
+ * ```
70
+ */
19
71
export const navigateToKinde = async ( {
20
72
url,
21
73
popupOptions = { } ,
@@ -33,6 +85,34 @@ export const navigateToKinde = async ({
33
85
}
34
86
} ;
35
87
88
+ /**
89
+ * Creates a popup window for Kinde authentication and waits for the result.
90
+ *
91
+ * This function opens a popup window with the specified URL and waits for
92
+ * a message from the popup containing authentication results. The popup
93
+ * window is configured with the provided options and positioned on screen.
94
+ *
95
+ * @param options - Configuration options for the popup
96
+ * @returns A promise that resolves with the popup window reference, or null if blocked
97
+ * @throws {Error } When the popup is blocked by the browser
98
+ * @throws {Error } When authentication fails
99
+ *
100
+ * @example
101
+ * ```typescript
102
+ * const popup = await createPopup({
103
+ * url: "https://app.kinde.com/oauth/authorize?client_id=...",
104
+ * popupOptions: {
105
+ * width: 600,
106
+ * height: 700,
107
+ * left: 100,
108
+ * top: 100
109
+ * },
110
+ * handleResult: async (result) => {
111
+ * console.log("Authentication completed:", result);
112
+ * }
113
+ * });
114
+ * ```
115
+ */
36
116
export const createPopup = async ( {
37
117
url,
38
118
popupOptions = { } ,
@@ -82,6 +162,25 @@ export const createPopup = async ({
82
162
return popup ;
83
163
} ;
84
164
165
+ /**
166
+ * Waits for a popup window to close by polling its closed state.
167
+ *
168
+ * This function creates a promise that resolves when the specified popup window
169
+ * is closed. It polls the popup's closed property every 100ms until the window
170
+ * is no longer open.
171
+ *
172
+ * @param popup - The popup window to monitor
173
+ * @returns A promise that resolves when the popup is closed
174
+ *
175
+ * @example
176
+ * ```typescript
177
+ * const popup = await createPopup({ url: "https://example.com" });
178
+ *
179
+ * // Wait for popup to close
180
+ * await waitForPopupClose(popup);
181
+ * console.log("Popup has been closed");
182
+ * ```
183
+ */
85
184
export const waitForPopupClose = ( popup : Window ) : Promise < void > => {
86
185
return new Promise ( ( resolve ) => {
87
186
const checkClosed = ( ) => {
0 commit comments