|
| 1 | +(() => { |
| 2 | + const { targetId, clientId, baseUrl } = getSetup(); |
| 3 | + |
| 4 | + // the code to verify login was not tampered with |
| 5 | + let code = ""; |
| 6 | + |
| 7 | + const USER_ADDRESS_KEY = "tw.login:userAddress"; |
| 8 | + const SESSION_KEY_ADDRESS_KEY = "tw.login:sessionKeyAddress"; |
| 9 | + |
| 10 | + function main() { |
| 11 | + // check if redirected first, this sets up the logged in state if it was from redirect |
| 12 | + const params = parseURLHash(new URL(window.location)); |
| 13 | + console.log(params); |
| 14 | + // TECHNICALLY this should verify the code... but can't do that without backend of some sort |
| 15 | + if (params) { |
| 16 | + // reset the code |
| 17 | + code = ""; |
| 18 | + // write the userAddress to local storage |
| 19 | + localStorage.setItem(USER_ADDRESS_KEY, params.userAddress); |
| 20 | + // write the sessionKeyAddress to local storage |
| 21 | + localStorage.setItem(SESSION_KEY_ADDRESS_KEY, params.sessionKeyAddress); |
| 22 | + // reset the URL hash |
| 23 | + window.location.hash = ""; |
| 24 | + } |
| 25 | + |
| 26 | + const userAddress = localStorage.getItem(USER_ADDRESS_KEY); |
| 27 | + const sessionKeyAddress = localStorage.getItem(SESSION_KEY_ADDRESS_KEY); |
| 28 | + |
| 29 | + if (userAddress && sessionKeyAddress) { |
| 30 | + // handle logged in state |
| 31 | + handleIsLoggedIn(); |
| 32 | + } else { |
| 33 | + // handle not logged in state |
| 34 | + handleNotLoggedIn(); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + function handleIsLoggedIn() { |
| 39 | + console.log("handleIsLoggedIn"); |
| 40 | + |
| 41 | + window.thirdweb = { |
| 42 | + isLoggedIn: true, |
| 43 | + getAddress: () => getAddress(), |
| 44 | + logout: () => { |
| 45 | + window.localStorage.removeItem(USER_ADDRESS_KEY); |
| 46 | + window.localStorage.removeItem(SESSION_KEY_ADDRESS_KEY); |
| 47 | + window.location.reload(); |
| 48 | + }, |
| 49 | + makeRequest: async () => { |
| 50 | + const res = await fetch(`${baseUrl}/api/request`, { |
| 51 | + method: "POST", |
| 52 | + body: JSON.stringify({ |
| 53 | + userAddress: getAddress(), |
| 54 | + sessionKeyAddress: getSessionKeyAddress(), |
| 55 | + }), |
| 56 | + }); |
| 57 | + const data = await res.json(); |
| 58 | + console.log(data); |
| 59 | + }, |
| 60 | + }; |
| 61 | + } |
| 62 | + |
| 63 | + function handleNotLoggedIn() { |
| 64 | + window.thirdweb = { login: onLogin, isLoggedIn: false }; |
| 65 | + } |
| 66 | + |
| 67 | + function onLogin() { |
| 68 | + code = window.crypto.getRandomValues(new Uint8Array(4)).join(""); |
| 69 | + // redirect to the login page |
| 70 | + const redirect = new URL(baseUrl); |
| 71 | + redirect.searchParams.set("code", code); |
| 72 | + redirect.searchParams.set("clientId", clientId); |
| 73 | + redirect.searchParams.set("redirect", window.location.href); |
| 74 | + window.location.href = redirect.href; |
| 75 | + } |
| 76 | + |
| 77 | + function getAddress() { |
| 78 | + return localStorage.getItem(USER_ADDRESS_KEY); |
| 79 | + } |
| 80 | + |
| 81 | + function getSessionKeyAddress() { |
| 82 | + return localStorage.getItem(SESSION_KEY_ADDRESS_KEY); |
| 83 | + } |
| 84 | + |
| 85 | + // utils |
| 86 | + |
| 87 | + function getSetup() { |
| 88 | + const el = document.currentScript; |
| 89 | + if (!el) { |
| 90 | + throw new Error("Could not find script element"); |
| 91 | + } |
| 92 | + const baseUrl = new URL(el.src).origin; |
| 93 | + const dataset = el.dataset; |
| 94 | + const targetId = dataset.target || "tw-login"; |
| 95 | + const clientId = dataset.clientId; |
| 96 | + if (!clientId) { |
| 97 | + throw new Error("Missing client-id"); |
| 98 | + } |
| 99 | + return { targetId, clientId, baseUrl }; |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * @param {URL} url |
| 104 | + * @returns null | { [key: string]: string } |
| 105 | + */ |
| 106 | + function parseURLHash(url) { |
| 107 | + if (!url.hash) { |
| 108 | + return null; |
| 109 | + } |
| 110 | + try { |
| 111 | + return decodeHash(url.hash); |
| 112 | + } catch { |
| 113 | + // if this fails, invalid data -> return null |
| 114 | + return null; |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Decodes a URL hash string to extract the three keys. |
| 120 | + * |
| 121 | + * @param {string} hash - A string like "#eyJrZXkxIjoiVmFsdWU..." |
| 122 | + * @returns {{ userAddress: string, sessionKeyAddress: string, code: string }} An object with the three keys |
| 123 | + */ |
| 124 | + function decodeHash(hash) { |
| 125 | + // Remove the "#" prefix, if present. |
| 126 | + const base64Data = hash.startsWith("#") ? hash.slice(1) : hash; |
| 127 | + |
| 128 | + // Decode the Base64 string, then parse the JSON. |
| 129 | + const jsonString = atob(base64Data); |
| 130 | + const data = JSON.parse(jsonString); |
| 131 | + |
| 132 | + // data should have the shape { userAddress, sessionKeyAddress, code }. |
| 133 | + if ( |
| 134 | + "userAddress" in data && |
| 135 | + "sessionKeyAddress" in data && |
| 136 | + "code" in data |
| 137 | + ) { |
| 138 | + return data; |
| 139 | + } |
| 140 | + return null; |
| 141 | + } |
| 142 | + |
| 143 | + main(); |
| 144 | +})(); |
0 commit comments