|
1 | 1 | (() => { |
2 | | - const { targetId, clientId, baseUrl } = getSetup(); |
3 | | - |
4 | | - // the code to verify login was not tampered with |
5 | | - let code = ""; |
| 2 | + const globalSetup = getSetup(); |
6 | 3 |
|
7 | 4 | const USER_ADDRESS_KEY = "tw.login:userAddress"; |
8 | 5 | const SESSION_KEY_ADDRESS_KEY = "tw.login:sessionKeyAddress"; |
| 6 | + const CODE_KEY = "tw.login:code"; |
9 | 7 |
|
10 | 8 | function main() { |
11 | 9 | // check if redirected first, this sets up the logged in state if it was from redirect |
12 | 10 | 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) { |
| 11 | + if (params && params.code === localStorage.getItem(CODE_KEY)) { |
| 12 | + // reset the URL hash |
| 13 | + window.location.hash = ""; |
16 | 14 | // reset the code |
17 | | - code = ""; |
| 15 | + localStorage.setItem(CODE_KEY, params.code); |
18 | 16 | // write the userAddress to local storage |
19 | 17 | localStorage.setItem(USER_ADDRESS_KEY, params.userAddress); |
20 | 18 | // write the sessionKeyAddress to local storage |
21 | 19 | localStorage.setItem(SESSION_KEY_ADDRESS_KEY, params.sessionKeyAddress); |
22 | | - // reset the URL hash |
23 | | - window.location.hash = ""; |
24 | 20 | } |
25 | 21 |
|
26 | 22 | const userAddress = localStorage.getItem(USER_ADDRESS_KEY); |
|
46 | 42 | window.localStorage.removeItem(SESSION_KEY_ADDRESS_KEY); |
47 | 43 | window.location.reload(); |
48 | 44 | }, |
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 | 45 | }; |
61 | 46 | } |
62 | 47 |
|
|
65 | 50 | } |
66 | 51 |
|
67 | 52 | function onLogin() { |
68 | | - code = window.crypto.getRandomValues(new Uint8Array(4)).join(""); |
| 53 | + const code = window.crypto.getRandomValues(new Uint8Array(4)).join(""); |
| 54 | + localStorage.setItem(CODE_KEY, code); |
69 | 55 | // redirect to the login page |
70 | | - const redirect = new URL(baseUrl); |
| 56 | + const redirect = new URL(globalSetup.baseUrl); |
71 | 57 | redirect.searchParams.set("code", code); |
72 | | - redirect.searchParams.set("clientId", clientId); |
| 58 | + redirect.searchParams.set("clientId", globalSetup.clientId); |
73 | 59 | redirect.searchParams.set("redirect", window.location.href); |
74 | 60 | window.location.href = redirect.href; |
75 | 61 | } |
|
78 | 64 | return localStorage.getItem(USER_ADDRESS_KEY); |
79 | 65 | } |
80 | 66 |
|
81 | | - function getSessionKeyAddress() { |
82 | | - return localStorage.getItem(SESSION_KEY_ADDRESS_KEY); |
83 | | - } |
84 | | - |
85 | 67 | // utils |
86 | | - |
87 | 68 | function getSetup() { |
88 | 69 | const el = document.currentScript; |
89 | 70 | if (!el) { |
90 | 71 | throw new Error("Could not find script element"); |
91 | 72 | } |
92 | 73 | const baseUrl = new URL(el.src).origin; |
93 | 74 | const dataset = el.dataset; |
94 | | - const targetId = dataset.target || "tw-login"; |
95 | 75 | const clientId = dataset.clientId; |
96 | 76 | if (!clientId) { |
97 | 77 | throw new Error("Missing client-id"); |
98 | 78 | } |
99 | | - return { targetId, clientId, baseUrl }; |
| 79 | + return { clientId, baseUrl }; |
100 | 80 | } |
101 | 81 |
|
102 | 82 | /** |
|
0 commit comments