From f28d643f31dc399384629de0178be68bcbfed108 Mon Sep 17 00:00:00 2001 From: YoanRos Date: Fri, 13 Sep 2024 10:35:58 +0200 Subject: [PATCH] fix: token set --- api/src/controllers/goal.js | 6 ++-- expo/src/Router.js | 2 +- expo/src/scenes/Auth/Signin.js | 1 + expo/src/scenes/Auth/Signup.js | 34 ++++++++++++------- .../scenes/WelcomeScreen/ScreensOldUser.js | 4 +-- 5 files changed, 29 insertions(+), 18 deletions(-) diff --git a/api/src/controllers/goal.js b/api/src/controllers/goal.js index deb038cd8..642f963f7 100644 --- a/api/src/controllers/goal.js +++ b/api/src/controllers/goal.js @@ -57,9 +57,9 @@ router.post( id: `${user.id}_${date}`, userId: user.id, date, - daysWithGoalNoDrink: user.goal_daysWithGoalNoDrink, - dosesByDrinkingDay: user.goal_dosesByDrinkingDay, - dosesPerWeek: user.goal_dosesPerWeek, + daysWithGoalNoDrink: daysWithGoalNoDrink, + dosesByDrinkingDay: dosesByDrinkingDay, + dosesPerWeek: dosesPerWeek, status: GoalStatus.InProgress, }, }); diff --git a/expo/src/Router.js b/expo/src/Router.js index e8a88be4a..cfc28429a 100644 --- a/expo/src/Router.js +++ b/expo/src/Router.js @@ -190,6 +190,7 @@ const App = () => { ); const [isTokenValid, setIsTokenValid] = useState(null); const token = storage.getString("@Token"); + API.setToken(token); useEffect(() => { const checkTokenValidity = async () => { @@ -201,7 +202,6 @@ const App = () => { try { const response = await API.get({ path: "/user/signin_token", - headers: { Authorization: `Bearer ${token}` }, }); if (response.ok) { diff --git a/expo/src/scenes/Auth/Signin.js b/expo/src/scenes/Auth/Signin.js index 6fcf16087..88d3a0e6e 100644 --- a/expo/src/scenes/Auth/Signin.js +++ b/expo/src/scenes/Auth/Signin.js @@ -51,6 +51,7 @@ const SigninScreen = ({ navigation }) => { }); if (response.ok) { API.setToken(response.token); + storage.set("@Token", response.token); if (!reconciliatedDrinksToDB) { await reconciliateDrinksToDB(); setReconciliatedDrinksToDB(true); diff --git a/expo/src/scenes/Auth/Signup.js b/expo/src/scenes/Auth/Signup.js index 48188f8af..a4b68b55e 100644 --- a/expo/src/scenes/Auth/Signup.js +++ b/expo/src/scenes/Auth/Signup.js @@ -25,12 +25,16 @@ import { migrateFromDailyGoalToWeekly, migrateMissingDrinkKey, } from "../../migrations"; +import RNRestart from "react-native-restart"; const SignupScreen = ({ navigation }) => { const [reconciliatedDrinksToDB, setReconciliatedDrinksToDB] = useState(false); const [reconciliatedGoalsToDB, setReconciliatedGoalsToDB] = useState(false); const [hasSentPreviousDrinksToDB, setHasSentPreviousDrinksToDB] = useState(hasSentPreviousDrinksToDB); const [hasCleanConsoAndCatalog, setHasCleanConsoAndCatalog] = useState(hasCleanConsoAndCatalog); + const [hasMigrateFromDailyGoalToWeekly, sethasMigrateFromDailyGoalToWeekly] = useState( + hasMigrateFromDailyGoalToWeekly + ); const [hasMigrateMissingDrinkKey, sethasMigrateMissingDrinkKey] = useState(hasMigrateMissingDrinkKey); const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); @@ -83,18 +87,24 @@ const SignupScreen = ({ navigation }) => { if (response.ok) { storage.set("@User", response.newUser.email); API.setToken(response.token); - await reconciliateDrinksToDB(); - setReconciliatedDrinksToDB(true); - await reconciliateGoalToDB(); - setReconciliatedGoalsToDB(true); - await cleanConsosAndCatalog(); - setHasCleanConsoAndCatalog(true); - await sendPreviousDrinksToDB(); - setHasSentPreviousDrinksToDB(true); - await migrateFromDailyGoalToWeekly(); - sethasMigrateFromDailyGoalToWeekly(true); - migrateMissingDrinkKey(); - sethasMigrateMissingDrinkKey(true); + storage.set("@Token", response.token); + const imported = storage.getBoolean("@imported"); + if (imported) { + await reconciliateDrinksToDB(); + setReconciliatedDrinksToDB(true); + await reconciliateGoalToDB(); + setReconciliatedGoalsToDB(true); + await cleanConsosAndCatalog(); + setHasCleanConsoAndCatalog(true); + await sendPreviousDrinksToDB(); + setHasSentPreviousDrinksToDB(true); + await migrateFromDailyGoalToWeekly(); + sethasMigrateFromDailyGoalToWeekly(true); + migrateMissingDrinkKey(); + sethasMigrateMissingDrinkKey(true); + RNRestart.Restart(); + } + navigation.navigate("EMAIL_CONFIRMATION"); } else { alert("Erreur lors de l'inscription"); diff --git a/expo/src/scenes/WelcomeScreen/ScreensOldUser.js b/expo/src/scenes/WelcomeScreen/ScreensOldUser.js index 64198c3d1..74921970e 100644 --- a/expo/src/scenes/WelcomeScreen/ScreensOldUser.js +++ b/expo/src/scenes/WelcomeScreen/ScreensOldUser.js @@ -9,7 +9,6 @@ import { logEvent } from "../../services/logEventsWithMatomo"; import { storage } from "../../services/storage"; import * as DocumentPicker from "expo-document-picker"; import { Alert } from "react-native"; -import RNRestart from "react-native-restart"; import API from "../../services/api"; import AntDesign from "@expo/vector-icons/AntDesign"; @@ -150,6 +149,7 @@ const overwriteData = async (dataImported, pushNotifToken) => { storage.clearAll(); if (pushNotifToken) { storage.set("STORAGE_KEY_PUSH_NOTIFICATION_TOKEN", pushNotifToken); + storage.set("@imported", true); } Object.keys(dataImported).forEach((key) => { const value = dataImported[key]; @@ -160,7 +160,7 @@ const overwriteData = async (dataImported, pushNotifToken) => { } }); - await API.put({ path: `/user`, body: { matomoId: dataImported["@UserIdv2"], pushNotifToken } }).then((res) => { + await API.put({ path: `/user`, body: { pushNotifToken } }).then((res) => { if (res.ok) { logEvent({ category: "TRANSFER", action: "IMPORT_DATA_SUCCESS" }); } else {