Skip to content

Commit 60c5c26

Browse files
authored
add thirdweb login client script (#6125)
1 parent dc74c3b commit 60c5c26

File tree

2 files changed

+12
-31
lines changed

2 files changed

+12
-31
lines changed

apps/login/public/tw-login.js renamed to apps/login/public/twl.js

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,22 @@
11
(() => {
2-
const { targetId, clientId, baseUrl } = getSetup();
3-
4-
// the code to verify login was not tampered with
5-
let code = "";
2+
const globalSetup = getSetup();
63

74
const USER_ADDRESS_KEY = "tw.login:userAddress";
85
const SESSION_KEY_ADDRESS_KEY = "tw.login:sessionKeyAddress";
6+
const CODE_KEY = "tw.login:code";
97

108
function main() {
119
// check if redirected first, this sets up the logged in state if it was from redirect
1210
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 = "";
1614
// reset the code
17-
code = "";
15+
localStorage.setItem(CODE_KEY, params.code);
1816
// write the userAddress to local storage
1917
localStorage.setItem(USER_ADDRESS_KEY, params.userAddress);
2018
// write the sessionKeyAddress to local storage
2119
localStorage.setItem(SESSION_KEY_ADDRESS_KEY, params.sessionKeyAddress);
22-
// reset the URL hash
23-
window.location.hash = "";
2420
}
2521

2622
const userAddress = localStorage.getItem(USER_ADDRESS_KEY);
@@ -46,17 +42,6 @@
4642
window.localStorage.removeItem(SESSION_KEY_ADDRESS_KEY);
4743
window.location.reload();
4844
},
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-
},
6045
};
6146
}
6247

@@ -65,11 +50,12 @@
6550
}
6651

6752
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);
6955
// redirect to the login page
70-
const redirect = new URL(baseUrl);
56+
const redirect = new URL(globalSetup.baseUrl);
7157
redirect.searchParams.set("code", code);
72-
redirect.searchParams.set("clientId", clientId);
58+
redirect.searchParams.set("clientId", globalSetup.clientId);
7359
redirect.searchParams.set("redirect", window.location.href);
7460
window.location.href = redirect.href;
7561
}
@@ -78,25 +64,19 @@
7864
return localStorage.getItem(USER_ADDRESS_KEY);
7965
}
8066

81-
function getSessionKeyAddress() {
82-
return localStorage.getItem(SESSION_KEY_ADDRESS_KEY);
83-
}
84-
8567
// utils
86-
8768
function getSetup() {
8869
const el = document.currentScript;
8970
if (!el) {
9071
throw new Error("Could not find script element");
9172
}
9273
const baseUrl = new URL(el.src).origin;
9374
const dataset = el.dataset;
94-
const targetId = dataset.target || "tw-login";
9575
const clientId = dataset.clientId;
9676
if (!clientId) {
9777
throw new Error("Missing client-id");
9878
}
99-
return { targetId, clientId, baseUrl };
79+
return { clientId, baseUrl };
10080
}
10181

10282
/**

apps/login/public/twl.min.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)