]> sipb.mit.edu Git - wiki.git/blob - names/code.js
add name change tool
[wiki.git] / names / code.js
1 // Force HTTPS - agnostic of which htaccess to use
2 // https://stackoverflow.com/questions/4723213/detect-http-or-https-then-force-https-in-javascript
3 if (location.protocol !== 'https:') {
4     location.replace(`https:${location.href.substring(location.protocol.length)}`);
5 }
6
7 // Temporary host: the non-production VM inside matrix.mit.edu
8 const API_HOST = "https://uplink.mit.edu";
9
10 document.getElementById("fingerToFull").addEventListener("click", function (ev) {
11     document.getElementById("fingerName").value = moira_user_info.full_name;
12 });
13
14 async function loadUserInfo() {
15     const response = await fetch(API_HOST + "/users/me/", {
16         headers: {
17             "Authorization": "webathena " + webathena_base64,
18         }
19     });
20     const json = await response.json();
21     console.log(json);
22     document.getElementById("displayName").innerText = json.full_name;
23
24     // Set global variable
25     moira_user_info = json;
26 }
27
28 async function loadFinger() {
29     const response = await fetch(API_HOST + "/users/me/finger", {
30         headers: {
31             "Authorization": "webathena " + webathena_base64,
32         }
33     });
34     const json = await response.json();
35     document.getElementById("fingerName").value = json.fullname;
36     console.log(json);
37
38     // Set global variable
39     moira_finger = json;
40 }
41
42 async function onLogin() {
43     document.getElementById("login").hidden = true;
44     await loadUserInfo();
45     await loadFinger();
46     document.getElementById("loading").hidden = true;
47     document.getElementById("names").hidden = false;
48 }
49
50 document.getElementById("apply").addEventListener("click", async function (ev) {
51     document.getElementById("applied").hidden = true;
52     document.getElementById("loading").hidden = false;
53     document.getElementById("names").hidden = true;
54     const input = {
55         fullname: document.getElementById("fingerName").value,
56     };
57     const response = await fetch(API_HOST + "/users/me/finger", {
58         method: "PATCH",
59         headers: {
60             "Authorization": "webathena " + webathena_base64,
61             "Content-Type": "application/json",
62         },
63         body: JSON.stringify(input),
64     });
65     console.log(response);
66     if (response.status === 200) {
67         await onLogin();
68         document.getElementById("applied").hidden = false;
69     } else {
70         const json = await(response.json());
71         console.log(json);
72         document.getElementById("loading").hidden = false;
73         alert(`An error occured: ${json.name}`);
74     }
75 });