Skip to content

Commit 7492352

Browse files
committed
Added storing user's cookies
1 parent 78f9b33 commit 7492352

3 files changed

Lines changed: 36 additions & 1 deletion

File tree

src/config/cookies

Whitespace-only changes.

src/lib/api/auth.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import axios from "axios"
22

3-
import { urls } from "../main"
3+
import { urls, setCookies, getCookies } from "../main"
44

55

66
/**
@@ -22,6 +22,8 @@ export const loginCodinGamer = async (email: string, password: string): Promise<
2222
data: [email, password, true, "CODINGAME"]
2323
})
2424

25+
setCookies(response["headers"])
26+
2527
return response["data"]
2628
}
2729

src/lib/main.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,38 @@
1+
const fs = require("fs")
2+
13
/** @internal */
24
export const urls = {
35
codingamer: "https://www.codingame.com/services/CodinGamer/",
46
puzzle: "https://www.codingame.com/services/Puzzle/"
7+
}
8+
9+
/** @internal */
10+
export const setCookies = (response: any) => {
11+
const cookies = response["set-cookie"]
12+
var cookiesStr = cookies.map((el: string, i: number) => {
13+
const elSplit = el.split(";")
14+
if (i !== cookies.length - 1) {
15+
return elSplit[0] + "; "
16+
} else {
17+
return elSplit[0]
18+
}
19+
}).join("")
20+
21+
fs.writeFile("./src/config/cookies", cookiesStr, function(err: any){
22+
if (err) {
23+
return console.log(err)
24+
}
25+
})
26+
}
27+
28+
/** @internal */
29+
export const getCookies = (): string => {
30+
try {
31+
const cookiesStr = fs.readFileSync("./src/config/cookies", "utf-8")
32+
console.log(cookiesStr)
33+
return cookiesStr
34+
} catch (e){
35+
console.log(e)
36+
return "Error"
37+
}
538
}

0 commit comments

Comments
 (0)