Skip to content

Commit 15d2c34

Browse files
FIX: cookie examples (#18)
* Fixed cookie example * Delete cookies_load.js * Change `cookies_store.js` to only store * Create cookies_load.js * Update cookies_store.js * Update cookies_load.js * Fix typo --------- Co-authored-by: JelleJurre <76777936+jellejurre@users.noreply.github.com>
1 parent 82ac310 commit 15d2c34

File tree

2 files changed

+37
-11
lines changed

2 files changed

+37
-11
lines changed

cookies_load.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,36 @@
11
const vrchat = require("vrchat");
22

3+
const readline = require("readline")
4+
5+
const tough = require("tough-cookie");
6+
const fs = require("fs");
7+
8+
const COOKIE_FILE = "cookies.json";
9+
let cookieJar = new tough.CookieJar();
10+
11+
if (fs.existsSync(COOKIE_FILE)) {
12+
const serializedCookies = fs.readFileSync(COOKIE_FILE, "utf-8");
13+
cookieJar = tough.CookieJar.deserializeSync(JSON.parse(serializedCookies));
14+
}
15+
316
const configuration = new vrchat.Configuration({
417
username: "username",
518
password: "password",
619
baseOptions: {
720
headers: {
8-
"User-Agent": "ExampleProgram/0.0.1 my@email.com"
9-
"Cookie": "auth=[AUTH_COOKIE_HERE]; twoFactorAuth=[TWO_FACTOR_AUTH_COOKIE_HERE]}"
10-
}
21+
"User-Agent": "ExampleProgram/0.0.1 my@email.com",
22+
// Use this instead of jar if you want to hard code cookies
23+
// "Cookie": "auth=[AUTH_COOKIE_HERE]; twoFactorAuth=[TWO_FACTOR_AUTH_COOKIE_HERE]"
24+
},
25+
jar: cookieJar,
1126
}
1227
});
1328

14-
const AuthenticationApi = new vrchat.AuthenticationApi(configuration);
29+
30+
const authenticationApi = new AuthenticationApi(configuration);
1531

1632
async function main() {
17-
const currentUser = (await AuthenticationApi.getCurrentUser()).data;
33+
var currentUser = (await authenticationApi.getCurrentUser()).data
1834
console.log(`Logged in as: ${currentUser.displayName}`);
1935
}
2036

cookies_store.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
const vrchat = require("vrchat");
22

33
const readline = require("readline")
4-
import globalAxios from "axios"
54

6-
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
5+
const tough = require("tough-cookie");
6+
const fs = require("fs");
7+
8+
const rl = readline.createInterface({input: process.stdin, output: process.stdout});
79
const prompt = (query) => new Promise((resolve) => rl.question(query, resolve));
810

911

12+
const COOKIE_FILE = "cookies.json";
13+
let cookieJar = new tough.CookieJar();
14+
1015
const configuration = new vrchat.Configuration({
1116
username: "username",
1217
password: "password",
1318
baseOptions: {
14-
headers: { "User-Agent": "ExampleProgram/0.0.1 my@email.com"}
19+
headers: { "User-Agent": "ExampleProgram/0.0.1 my@email.com"},
20+
jar: cookieJar,
1521
}
1622
});
1723

@@ -32,9 +38,13 @@ async function main() {
3238

3339
console.log(`Logged in as: ${currentUser.displayName}`);
3440

35-
const store = globalAxios.defaults.jar.store.idx["api.vrchat.cloud"]["/"];
36-
console.log(`auth=${store["auth"]["value"]}`)
37-
console.log(`twoFactorAuth=${store["twoFactorAuth"]["value"]}`)
41+
const serializedJar = JSON.stringify(cookieJar.serializeSync());
42+
fs.writeFileSync(COOKIE_FILE, serializedJar);
43+
44+
const deserializedJar = tough.CookieJar.deserializeSync(serializedJar);
45+
const store = deserializedJar.store.idx["api.vrchat.cloud"]["/"];
46+
console.log(`auth=${store["auth"]["value"]}`);
47+
console.log(`twoFactorAuth=${store["twoFactorAuth"]["value"]}`);
3848
}
3949

4050
main();

0 commit comments

Comments
 (0)