-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
64 lines (52 loc) · 1.55 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const params = new URLSearchParams(location.search);
if (!params.has("state")) {
auth.remove();
const response = await fetch("/services.json");
if (response.ok) {
const backends = await response.json();
backend_list.innerHTML = Object.entries(backends).map(([name, meta]) => {
let id = name.toLowerCase();
return `
<li class="${id} backend">
<img src="${ meta.icon ?? "img/default-logo.svg" }" alt="${name} logo" />
<a href="https://madata.dev/backends/${id}">${name}</a>
</li>`
}).join("\n");
}
}
else {
supported.remove();
const state = JSON.parse(params.get("state"));
const url = state.url;
const backend = state.backend;
const code = params.get("code");
if (backend && url && code) {
auth.removeAttribute("hidden");
const appURL = document.querySelector("#url");
appURL.textContent = url;
appURL.href = url;
backend_name.textContent = backend;
const response = await fetch("/.netlify/functions/auth", {
method: "POST",
body: JSON.stringify({ backend, code, redirectURI: params.get("redirect_uri") })
});
const json = await response.json();
yes_button.addEventListener("click", async () => {
const token = json.token;
if (!token) {
throw new Error("We could not obtain an access token!");
}
else {
opener.postMessage({ backend, token }, url);
}
window.close();
});
no_button.addEventListener("click", async () => {
window.close();
});
}
else {
// TODO better error message
document.body.textContent = "The URL you provided is invalid. You can't be authenticated!";
}
}