Skip to content

Commit 64d3ed0

Browse files
committed
add thirdweb login client script
1 parent dc74c3b commit 64d3ed0

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed

apps/login/public/twl.js

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

0 commit comments

Comments
 (0)