-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetToken.js
More file actions
62 lines (51 loc) · 1.69 KB
/
getToken.js
File metadata and controls
62 lines (51 loc) · 1.69 KB
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
60
61
62
import fs from "fs";
import fetch from "node-fetch";
import { logger } from "./utils/logger.js";
// Path to the accounts file and token file
const filePath = "./accounts.txt";
const tokenFilePath = "./token.txt";
// Function to read and parse accounts
function readAccounts() {
let accounts = [];
let data = fs
.readFileSync("accounts.txt", "utf-8")
.split("\n")
.filter((data) => data.trim() !== "");
if (!data.length) return [];
for (let i = 0; i < data.length; i++) {
const res = data[i].split("|");
accounts.push({ email: res[0], password: res[1] });
}
return accounts;
}
async function getToken() {
if (fs.existsSync(tokenFilePath)) {
fs.unlinkSync(tokenFilePath);
logger("Existing token.txt removed.");
}
const accounts = readAccounts();
for (const { email, password } of accounts) {
logger(`Email: ${email}, Password: ${password}`);
try {
const loginPayload = { username: email, password };
const loginResponse = await fetch("https://api.openloop.so/users/login", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(loginPayload),
});
if (!loginResponse.ok) {
throw new Error(`Login failed! Status: ${loginResponse.status}`);
}
const loginData = await loginResponse.json();
const accessToken = loginData.data.accessToken;
logger("Login successful, Token:", "success", accessToken);
fs.appendFileSync(tokenFilePath, accessToken + "\n", "utf8");
logger("Access token saved to token.txt");
} catch (error) {
logger("Error during login:", "error", error.message);
}
}
}
export default getToken;