1 | import {cognitoLoginUrl, clientId} from "./utils.mjs"; |
2 | import ReactDOM from "react-dom"; |
3 | import {App} from "./App.mjs"; |
4 | import {processLoginFlow, redirectToLogin, redirectToLogout, getValidTokens } from "./cognito-utils.mjs"; |
5 | import htm from "htm"; |
6 | import React from "react"; |
7 | |
8 | const html = htm.bind(React.createElement); |
9 | |
10 | const serialize = (fn) => { |
11 | let queue = Promise.resolve(); |
12 | return (...args) => { |
13 | const res = queue.then(() => fn(...args)); |
14 | queue = res.catch(() => {}); |
15 | return res; |
16 | }; |
17 | }; |
18 | |
19 | processLoginFlow(cognitoLoginUrl, clientId)((initialTokens) => { |
20 | const getAccessToken = ((initialTokens) => { |
21 | let tokens = initialTokens; |
22 | return serialize(async () => { |
23 | tokens = await getValidTokens(cognitoLoginUrl, clientId, tokens); |
24 | return tokens.access_token; |
25 | }); |
26 | })(initialTokens); |
27 | |
28 | ReactDOM.createRoot(document.getElementById("content")).render(html` |
29 | <${App} |
30 | loggedin=${!!initialTokens} |
31 | getAccessToken=${getAccessToken} |
32 | redirectToLogin=${() => redirectToLogin(cognitoLoginUrl, clientId)} |
33 | redirectToLogout=${() => redirectToLogout(cognitoLoginUrl, clientId)} |
34 | /> |
35 | `); |
36 | }); |
37 | |
38 | |