Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Achaak committed Jun 27, 2023
1 parent 0e92897 commit 2baae90
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 15 deletions.
10 changes: 10 additions & 0 deletions src/lib/dataManager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class DataManager {

access_token: string | null;
refresh_token: string | null;
cookie: string | string[] | null;
userId: number | null;

lastRender: number | undefined;
Expand All @@ -42,6 +43,7 @@ class DataManager {

this.access_token = null;
this.refresh_token = null;
this.cookie = null;
this.userId = null;

this.lastRender = undefined;
Expand Down Expand Up @@ -79,10 +81,12 @@ class DataManager {
access_token,
refresh_token,
userId,
cookie,
}: {
access_token?: string;
refresh_token?: string;
userId?: number;
cookie?: string | string[];
}) {
if (access_token) {
this.access_token = access_token;
Expand All @@ -96,6 +100,10 @@ class DataManager {
this.userId = userId;
await setData("userId", userId);
}
if (cookie) {
this.cookie = cookie;
await setData("cookie", cookie);
}
}

// Auth by email (send polling id by email)
Expand Down Expand Up @@ -137,6 +145,7 @@ class DataManager {
access_token: data.access_token,
refresh_token: data.refresh_token,
userId: data.startup_data.user.user_id,
cookie: res.headers["Set-Cookie"],
});
console.log("You are connected.");

Expand Down Expand Up @@ -211,6 +220,7 @@ class DataManager {
this.setAuth({
access_token: data.access_token,
refresh_token: data.refresh_token,
cookie: res.headers["Set-Cookie"],
});
}
});
Expand Down
17 changes: 12 additions & 5 deletions src/services/API/config.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import { gotScraping } from "got-scraping";

export const BASE_URL = 'https://apptoogoodtogo.com/api/';
export const API_ITEM_ENDPOINT = 'item/v8/';
export const AUTH_BY_EMAIL_ENDPOINT = 'auth/v3/authByEmail';
export const AUTH_POLLING_ENDPOINT = 'auth/v3/authByRequestPollingId';
export const REFRESH_ENDPOINT = 'auth/v3/token/refresh';
export const USER_AGENT = "TGTG/22.5.5 Dalvik/2.1.0 (Linux; Android 12; SM-G920V Build/MMB29K)";
export const DEVICE_TYPE = "ANDROID";

export const api = gotScraping.extend({
prefixUrl: "https://apptoogoodtogo.com/api/",
prefixUrl: BASE_URL,
headers: {
"User-Agent":
"TGTG/22.5.5 Dalvik/2.1.0 (Linux; Android 12; SM-G920V Build/MMB29K)",
"User-Agent": USER_AGENT,
"Content-Type": "application/json; charset=utf-8",
Accept: "application/json",
"Accept-Language": "en-US",
"Accept-Language": "en-UK",
"Accept-Encoding": "gzip",
},
responseType: "json",
Expand All @@ -20,6 +27,6 @@ export const api = gotScraping.extend({
],
devices: ["mobile"],
locales: ["en-US"],
operatingSystems: ["ios"],
operatingSystems: ["android"],
},
});
14 changes: 7 additions & 7 deletions src/services/API/connect.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { api } from "./config.js";
import { AUTH_BY_EMAIL_ENDPOINT, AUTH_POLLING_ENDPOINT, DEVICE_TYPE, REFRESH_ENDPOINT, api } from "./config.js";
import { env } from './../../env/server.js';

export const authByEmail = () =>
api.post<{
state: string;
polling_id: string;
}>("auth/v3/authByEmail", {
}>(AUTH_BY_EMAIL_ENDPOINT, {
json: {
device_type: "ANDROID",
device_type: DEVICE_TYPE,
email: env.CREDENTIAL_EMAIL,
},
});
Expand All @@ -21,9 +21,9 @@ export const authByRequestPollingId = ({
access_token: string;
refresh_token: string;
startup_data: { user: { user_id: number } };
}>("auth/v3/authByRequestPollingId", {
}>(AUTH_POLLING_ENDPOINT, {
json: {
device_type: "ANDROID",
device_type: DEVICE_TYPE,
email: env.CREDENTIAL_EMAIL,
request_polling_id: polling_id,
},
Expand All @@ -33,9 +33,9 @@ export const refresh = ({ refreshToken }: { refreshToken: string }) =>
api.post<{
access_token: string;
refresh_token: string;
}>("auth/v3/token/refresh", {
}>(REFRESH_ENDPOINT, {
json: {
device_type: "ANDROID",
device_type: DEVICE_TYPE,
refresh_token: refreshToken,
},
});
6 changes: 3 additions & 3 deletions src/services/API/item.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Favorite } from "./../../types/favorite.js";
import { api } from "./config.js";
import { API_ITEM_ENDPOINT, DEVICE_TYPE, api } from "./config.js";

export const getFavorite = ({
userId,
Expand All @@ -8,7 +8,7 @@ export const getFavorite = ({
userId: number;
accessToken: string;
}) =>
api.post<{ items: Favorite[] }>("item/v8/", {
api.post<{ items: Favorite[] }>(API_ITEM_ENDPOINT, {
json: {
favorites_only: true,
origin: {
Expand All @@ -17,7 +17,7 @@ export const getFavorite = ({
},
radius: 200,
user_id: userId,
device_type: "ANDROID",
device_type: DEVICE_TYPE,
},
headers: {
Authorization: `Bearer ${accessToken}`,
Expand Down

0 comments on commit 2baae90

Please sign in to comment.