|
2 | 2 | (function () { |
3 | 3 | const globalSetup = getSetup(); |
4 | 4 |
|
5 | | - const USER_ADDRESS_KEY = "tw.login:userAddress"; |
6 | | - const SESSION_KEY_ADDRESS_KEY = "tw.login:sessionKeyAddress"; |
| 5 | + const JWT_KEY = "tw.login:jwt"; |
7 | 6 | const CODE_KEY = "tw.login:code"; |
8 | 7 |
|
9 | 8 | function main() { |
10 | 9 | // check if redirected first, this sets up the logged in state if it was from redirect |
11 | | - const params = parseURLHash(new URL(window.location)); |
12 | | - if (params && params.code === localStorage.getItem(CODE_KEY)) { |
13 | | - // reset the URL hash |
| 10 | + const result = parseURL(new URL(window.location)); |
| 11 | + console.log(result); |
| 12 | + if ( |
| 13 | + result && |
| 14 | + result.length === 2 && |
| 15 | + result[1] === localStorage.getItem(CODE_KEY) |
| 16 | + ) { |
| 17 | + // reset the URL |
14 | 18 | window.location.hash = ""; |
15 | | - // reset the code |
16 | | - localStorage.setItem(CODE_KEY, params.code); |
17 | | - // write the userAddress to local storage |
18 | | - localStorage.setItem(USER_ADDRESS_KEY, params.userAddress); |
19 | | - // write the sessionKeyAddress to local storage |
20 | | - localStorage.setItem(SESSION_KEY_ADDRESS_KEY, params.sessionKeyAddress); |
| 19 | + window.location.search = ""; |
| 20 | + |
| 21 | + // write the jwt to local storage |
| 22 | + localStorage.setItem(JWT_KEY, result[0]); |
21 | 23 | } |
22 | 24 |
|
23 | | - const userAddress = localStorage.getItem(USER_ADDRESS_KEY); |
24 | | - const sessionKeyAddress = localStorage.getItem(SESSION_KEY_ADDRESS_KEY); |
| 25 | + // always reset the code |
| 26 | + localStorage.removeItem(CODE_KEY); |
25 | 27 |
|
26 | | - if (userAddress && sessionKeyAddress) { |
| 28 | + const jwt = localStorage.getItem(JWT_KEY); |
| 29 | + |
| 30 | + if (jwt) { |
27 | 31 | // handle logged in state |
28 | 32 | handleIsLoggedIn(); |
29 | 33 | } else { |
|
37 | 41 |
|
38 | 42 | window.thirdweb = { |
39 | 43 | isLoggedIn: true, |
40 | | - getAddress: () => getAddress(), |
| 44 | + getUser: async () => { |
| 45 | + const res = await fetch(`${globalSetup.baseUrl}/api/user`, { |
| 46 | + headers: { |
| 47 | + Authorization: `Bearer ${localStorage.getItem(JWT_KEY)}`, |
| 48 | + }, |
| 49 | + }); |
| 50 | + return res.json(); |
| 51 | + }, |
41 | 52 | logout: () => { |
42 | | - window.localStorage.removeItem(USER_ADDRESS_KEY); |
43 | | - window.localStorage.removeItem(SESSION_KEY_ADDRESS_KEY); |
| 53 | + window.localStorage.removeItem(JWT_KEY); |
44 | 54 | window.location.reload(); |
45 | 55 | }, |
46 | 56 | }; |
|
51 | 61 | } |
52 | 62 |
|
53 | 63 | function onLogin() { |
54 | | - const code = window.crypto.getRandomValues(new Uint8Array(4)).join(""); |
| 64 | + const code = window.crypto.getRandomValues(new Uint8Array(16)).join(""); |
55 | 65 | localStorage.setItem(CODE_KEY, code); |
56 | 66 | // redirect to the login page |
57 | 67 | const redirect = new URL(globalSetup.baseUrl); |
58 | 68 | redirect.searchParams.set("code", code); |
59 | 69 | redirect.searchParams.set("clientId", globalSetup.clientId); |
60 | | - redirect.searchParams.set("redirect", window.location.href); |
| 70 | + redirect.searchParams.set( |
| 71 | + "redirect", |
| 72 | + window.location.origin + window.location.pathname, |
| 73 | + ); |
61 | 74 | window.location.href = redirect.href; |
62 | 75 | } |
63 | 76 |
|
64 | | - function getAddress() { |
65 | | - return localStorage.getItem(USER_ADDRESS_KEY); |
66 | | - } |
67 | | - |
68 | 77 | // utils |
69 | 78 | function getSetup() { |
70 | 79 | const el = document.currentScript; |
|
82 | 91 |
|
83 | 92 | /** |
84 | 93 | * @param {URL} url |
85 | | - * @returns null | { [key: string]: string } |
| 94 | + * @returns null | [string, string] |
86 | 95 | */ |
87 | | - function parseURLHash(url) { |
88 | | - if (!url.hash) { |
89 | | - return null; |
90 | | - } |
| 96 | + function parseURL(url) { |
91 | 97 | try { |
92 | | - return decodeHash(url.hash); |
| 98 | + const hash = url.hash.startsWith("#") ? url.hash.slice(1) : url.hash; |
| 99 | + const code = url.searchParams.get("code"); |
| 100 | + if (!hash || !code) { |
| 101 | + return null; |
| 102 | + } |
| 103 | + return [hash, code]; |
93 | 104 | } catch { |
94 | 105 | // if this fails, invalid data -> return null |
95 | 106 | return null; |
96 | 107 | } |
97 | 108 | } |
98 | 109 |
|
99 | | - /** |
100 | | - * Decodes a URL hash string to extract the three keys. |
101 | | - * |
102 | | - * @param {string} hash - A string like "#eyJrZXkxIjoiVmFsdWU..." |
103 | | - * @returns {{ userAddress: string, sessionKeyAddress: string, code: string }} An object with the three keys |
104 | | - */ |
105 | | - function decodeHash(hash) { |
106 | | - // Remove the "#" prefix, if present. |
107 | | - const base64Data = hash.startsWith("#") ? hash.slice(1) : hash; |
108 | | - |
109 | | - // Decode the Base64 string, then parse the JSON. |
110 | | - const jsonString = atob(base64Data); |
111 | | - const data = JSON.parse(jsonString); |
112 | | - |
113 | | - // data should have the shape { userAddress, sessionKeyAddress, code }. |
114 | | - if ( |
115 | | - "userAddress" in data && |
116 | | - "sessionKeyAddress" in data && |
117 | | - "code" in data |
118 | | - ) { |
119 | | - return data; |
120 | | - } |
121 | | - return null; |
122 | | - } |
123 | | - |
124 | 110 | main(); |
125 | 111 | })(); |
0 commit comments