From 8d4301a0c0598decab13b1b40c594c938edf36bb Mon Sep 17 00:00:00 2001 From: Reckless_Satoshi <90936742+Reckless-Satoshi@users.noreply.github.com> Date: Sat, 4 Mar 2023 12:54:06 +0000 Subject: [PATCH] Fix Native requests (#375) * fix-android-maker * test --- frontend/src/basic/RobotPage/index.tsx | 6 ++++-- frontend/src/contexts/AppContext.tsx | 4 +++- .../src/services/api/ApiNativeClient/index.ts | 21 +++++++++++-------- mobile/App.tsx | 1 + 4 files changed, 20 insertions(+), 12 deletions(-) diff --git a/frontend/src/basic/RobotPage/index.tsx b/frontend/src/basic/RobotPage/index.tsx index 4eb32535..e14c972b 100644 --- a/frontend/src/basic/RobotPage/index.tsx +++ b/frontend/src/basic/RobotPage/index.tsx @@ -43,9 +43,11 @@ const RobotPage = (): JSX.Element => { setInputToken(robot.token); } if (robot.nickname == null && robot.token) { - fetchRobot({ action: 'generate', setBadRequest }); + if (window.NativeRobosats === undefined || torStatus == '"Done"') { + fetchRobot({ action: 'generate', setBadRequest }); + } } - }, []); + }, [torStatus]); const getGenerateRobot = (token: string, slot?: number) => { setInputToken(token); diff --git a/frontend/src/contexts/AppContext.tsx b/frontend/src/contexts/AppContext.tsx index 97568032..472bde32 100644 --- a/frontend/src/contexts/AppContext.tsx +++ b/frontend/src/contexts/AppContext.tsx @@ -18,6 +18,7 @@ import { } from '../models'; import { apiClient } from '../services/api'; +import { systemClient } from '../services/System'; import { checkVer, getHost, tokenStrength } from '../utils'; import { sha256 } from 'js-sha256'; @@ -252,7 +253,7 @@ export const AppContextProvider = ({ useEffect(() => { window.addEventListener('torStatus', (event) => { // UX improv: delay the "Conencted" status by 10 secs to avoid long waits for first requests - setTimeout(() => setTorStatus(event?.detail), event?.detail === '"Done"' ? 10000 : 0); + setTimeout(() => setTorStatus(event?.detail), event?.detail === '"Done"' ? 5000 : 0); }); }, []); @@ -465,6 +466,7 @@ export const AppContextProvider = ({ setRobot(newRobot); garage.updateRobot(newRobot, targetSlot); setCurrentSlot(targetSlot); + systemClient.setItem('robot_token', token); } }); }; diff --git a/frontend/src/services/api/ApiNativeClient/index.ts b/frontend/src/services/api/ApiNativeClient/index.ts index f091ef23..643deb25 100644 --- a/frontend/src/services/api/ApiNativeClient/index.ts +++ b/frontend/src/services/api/ApiNativeClient/index.ts @@ -10,16 +10,19 @@ class ApiNativeClient implements ApiClient { 'Content-Type': 'application/json', }; - const sessionid = systemClient.getCookie('sessionid'); - const csrftoken = systemClient.getCookie('csrftoken'); + const robotToken = systemClient.getItem('robot_token'); + if (robotToken) { + const sessionid = systemClient.getCookie('sessionid'); + const csrftoken = systemClient.getCookie('csrftoken'); - headers = { - ...headers, - ...{ - 'X-CSRFToken': csrftoken, - Cookie: `sessionid=${sessionid};token=${csrftoken}`, - }, - }; + headers = { + ...headers, + ...{ + 'X-CSRFToken': csrftoken, + Cookie: `sessionid=${sessionid};csrftoken=${csrftoken}`, + }, + }; + } return headers; }; diff --git a/mobile/App.tsx b/mobile/App.tsx index ac1387e9..92d0e4de 100644 --- a/mobile/App.tsx +++ b/mobile/App.tsx @@ -45,6 +45,7 @@ const App = () => { EncryptedStorage.removeItem('sessionid'); EncryptedStorage.removeItem('csrftoken'); + loadCookie('robot_token'); loadCookie('settings_fontsize_basic'); loadCookie('settings_language'); loadCookie('settings_mode');