-
Notifications
You must be signed in to change notification settings - Fork 5.9k
/
Copy pathapp.ts
51 lines (42 loc) · 1.58 KB
/
app.ts
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
//@ts-ignore
import { MDCTextField } from "@material/textfield";
//@ts-ignore
import { MDCCheckbox } from "@material/checkbox";
import { createHash } from "crypto";
import "material-components-web/dist/material-components-web.css";
import "./app.scss";
document.querySelectorAll(".mdc-text-field").forEach((d) => new MDCTextField(d));
document.querySelectorAll(".mdc-checkbox").forEach((d) => new MDCCheckbox(d));
window.addEventListener("message", (event) => {
if (event.data === "app") {
document.body.classList.add("in-app");
const back = document.querySelector(".back")!;
back.addEventListener("click", () => {
(event.source as Window).postMessage("back", event.origin);
});
}
});
const password = document.getElementById("password") as HTMLInputElement;
const form = document.getElementById("login-form") as HTMLFormElement;
if (!form) {
throw new Error("No password form found");
}
const hash = (guid: string): string => {
return createHash("sha256").update(guid).digest("hex");
};
form.addEventListener("submit", (e) => {
e.preventDefault();
document.cookie = `password=${hash(password.value)}; `
+ `path=${location.pathname.replace(/\/login\/?$/, "/")}; `
+ `domain=${location.hostname}`;
location.reload();
});
/**
* Notify user on load of page if previous password was unsuccessful
*/
const reg = new RegExp(`password=(\\w+);?`);
const matches = document.cookie.match(reg);
const errorDisplay = document.getElementById("error-display") as HTMLDivElement;
if (document.referrer === document.location.href && matches) {
errorDisplay.innerText = "Password is incorrect!";
}