generated from homebridge/homebridge-plugin-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.js
59 lines (52 loc) · 1.46 KB
/
auth.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
// eslint-disable-next-line @typescript-eslint/no-var-requires
const readline = require("readline");
function read(prompt) {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
return new Promise((resolve) => {
rl.question(prompt, function (response) {
resolve(response);
rl.close();
});
});
}
async function main() {
let response = await fetch("https://api-user.e2ro.com/2.2/login", {
method: "POST",
headers: {
"content-type": "application/json",
},
body: JSON.stringify({
login: await read(
"login identifier (email or phone, amazon login not supported): ",
),
}),
});
if (!response.ok) {
throw new Error("failed to send login request");
}
if (response.status !== 200) {
throw new Error(`failed to make login request: ${response.status}`);
}
const {
data: { user_token },
} = await response.json();
response = await fetch("https://api-user.e2ro.com/2.2/login/verify", {
method: "POST",
headers: {
"content-type": "application/json",
cookie: `s=${user_token}`,
},
body: JSON.stringify({ code: await read("verification code: ") }),
});
if (!response.ok) {
throw new Error("failed to send verification request");
}
if (response.status !== 200) {
throw new Error(`failed to make verification request: ${response.status}`);
}
process.stdout.write(`user token: ${user_token}\n`);
}
main();