-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.ts
59 lines (55 loc) · 1.19 KB
/
auth.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
52
53
54
55
56
57
58
59
const AUTH_SERVER = Deno.env.get("AUTH_SERVER");
if(AUTH_SERVER === "") {
console.error("No endpoint set!")
}
export const auth = async (token: string) => {
return (await fetch(
AUTH_SERVER + "/auth",
{ headers: { "x-access-token": token } }
)
.then(r => r.json())
.then(b => b.username))
};
export const register = async (username: string, password: string) => {
let failed: any;
let token: any;
const body = { username, password };
(await fetch(AUTH_SERVER + "/users", {
method: "POST",
headers: {
"content-type": "application/json"
},
body: JSON.stringify(body)
})
.then(async r => r.json())
.then(b => {
if (b.msg) {
failed = b;
}
}).catch(e => {
console.error(e)
}));
if (failed) {
console.log("FAILED1");
return failed;
}
(await fetch(AUTH_SERVER + `/users/${username}`, {
method: "POST",
body: JSON.stringify({ password })
})
.then(r => r.json())
.then(b => {
if (b.msg) {
failed = b;
} else {
token = b.jwt;
}
}).catch(e => {
console.error(e)
}));
if (failed) {
console.log("FAILED2");
return failed;
}
return token;
};