-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
39 lines (35 loc) · 993 Bytes
/
main.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
import "./style.css";
import { Elm } from "./src/Main.elm";
if (process.env.NODE_ENV === "development") {
const ElmDebugTransform = await import("elm-debug-transformer");
ElmDebugTransform.register({
simple_mode: true,
});
}
const root = document.querySelector("#app div");
const r = localStorage.getItem("auth");
(async () => {
const user = async (token) => {
if (token === "") return null;
const f = await fetch("http://localhost:3000/is_auth", {
method: "GET",
headers: { auth: token },
});
const r = await f.json();
if (r.login && r.user) {
console.log(JSON.parse(r.user));
return JSON.parse(r.user);
}
return null;
};
const app = Elm.Main.init({
node: root,
flags: { token: r ? r : "", user: await user(r) },
});
app.ports.setStorage.subscribe(function(state) {
localStorage.setItem("auth", state);
});
app.ports.removeItem.subscribe(function() {
localStorage.removeItem("auth");
});
})();