forked from Zlkcyber/openloop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetToken.js
62 lines (51 loc) · 1.89 KB
/
getToken.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
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(filePath) {
const accounts = [];
const data = fs.readFileSync(filePath, 'utf-8');
data.split('\n').forEach((line) => {
const match = line.match(/Email:\s*(.+?),\s*Password:\s*(.+)/);
if (match) {
const email = match[1];
const password = match[2];
accounts.push({ email, password });
}
});
return accounts;
}
async function getToken() {
if (fs.existsSync(tokenFilePath)) {
fs.unlinkSync(tokenFilePath);
logger('Existing token.txt removed.');
}
const accounts = readAccounts(filePath);
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;