Commit 53375ad1 by G

fix: disable automatic login from old authentication context

parent 3f310bfb
import cacheAssetsAsync from "@/utils/assetsCache";
import authenticateUser, { parseAuthicationErrors } from "@/utils/requests/authenticateUser";
import type { IuserInformations } from "@/utils/requests/types";
import getUserInformations, {
parseUserInformationsErrors,
} from "@/utils/requests/userInformations";
import ErrorModal from "@components/modals/ErrorModal"; import ErrorModal from "@components/modals/ErrorModal";
import { LOG } from "@logger"; import { LOG } from "@logger";
import AsyncStorage from "@react-native-async-storage/async-storage"; import AsyncStorage from "@react-native-async-storage/async-storage";
// import { type NavigationProp, useNavigation } from "@react-navigation/native";
import { useMutation } from "@tanstack/react-query"; import { useMutation } from "@tanstack/react-query";
import * as SplashScreen from "expo-splash-screen"; import * as SplashScreen from "expo-splash-screen";
import { createContext, useCallback, useContext, useEffect, useState } from "react"; import { createContext, useCallback, useContext, useState } from "react";
// import { type NavigationProp, useNavigation } from "@react-navigation/native";
import authenticateUser, { parseAuthicationErrors } from "@/utils/requests/authenticateUser";
import type { IuserInformations } from "@/utils/requests/types";
import getUserInformations, {
parseUserInformationsErrors,
} from "@/utils/requests/userInformations";
import { useModalsManagerContext } from "./ModalsManagerContext"; import { useModalsManagerContext } from "./ModalsManagerContext";
import type { IauthenticationData } from "./Types"; import type { IauthenticationData } from "./Types";
...@@ -69,7 +68,7 @@ export const UserAuthenticationContextProvider = ({ children }: { children: Reac ...@@ -69,7 +68,7 @@ export const UserAuthenticationContextProvider = ({ children }: { children: Reac
access: "", access: "",
refresh: "", refresh: "",
}); });
const [error, setError] = useState(""); const [_error, setError] = useState("");
const [userInformations, setUserInformations] = useState<IuserInformations>({ const [userInformations, setUserInformations] = useState<IuserInformations>({
username: "JohnDoe", username: "JohnDoe",
email: "JohnDoe@example.com", email: "JohnDoe@example.com",
...@@ -120,6 +119,15 @@ export const UserAuthenticationContextProvider = ({ children }: { children: Reac ...@@ -120,6 +119,15 @@ export const UserAuthenticationContextProvider = ({ children }: { children: Reac
}, },
}); });
const clearStorages = useCallback(async () => {
try {
await AsyncStorage.clear();
} catch (error) {
log.error("clearStorages |", JSON.stringify(error, null, 2));
// saving error
}
}, []);
const userInformationsMutation = useMutation({ const userInformationsMutation = useMutation({
mutationFn: (userAccessToken: string) => getUserInformations(userAccessToken), mutationFn: (userAccessToken: string) => getUserInformations(userAccessToken),
onMutate: () => { onMutate: () => {
...@@ -187,7 +195,7 @@ export const UserAuthenticationContextProvider = ({ children }: { children: Reac ...@@ -187,7 +195,7 @@ export const UserAuthenticationContextProvider = ({ children }: { children: Reac
}); });
await clearStorages(); await clearStorages();
})(); })();
}, []); }, [clearStorages]);
// Storages // Storages
...@@ -209,59 +217,50 @@ export const UserAuthenticationContextProvider = ({ children }: { children: Reac ...@@ -209,59 +217,50 @@ export const UserAuthenticationContextProvider = ({ children }: { children: Reac
} }
}; };
const clearStorages = async () => { const _loadAuthenticationData = async () => {
try {
await AsyncStorage.clear();
} catch (error) {
log.error("clearStorages |", JSON.stringify(error, null, 2));
// saving error
}
};
const loadAuthenticationData = async () => {
log.debug("loadAuthenticationData | Loading authentication data"); log.debug("loadAuthenticationData | Loading authentication data");
const jsonRepresentation = await AsyncStorage.getItem("authenticationData"); const jsonRepresentation = await AsyncStorage.getItem("authenticationData");
return jsonRepresentation ? JSON.parse(jsonRepresentation) : null; return jsonRepresentation ? JSON.parse(jsonRepresentation) : null;
}; };
const loadUserInformations = async () => { const _loadUserInformations = async () => {
log.debug("loadUserInformations | Loading user informations"); log.debug("loadUserInformations | Loading user informations");
const jsonRepresentation = await AsyncStorage.getItem("userInformations"); const jsonRepresentation = await AsyncStorage.getItem("userInformations");
return jsonRepresentation ? JSON.parse(jsonRepresentation) : null; return jsonRepresentation ? JSON.parse(jsonRepresentation) : null;
}; };
// biome-ignore lint/correctness/useExhaustiveDependencies: <This should only be executed once. At startup.> // biome-ignore lint/correctness/useExhaustiveDependencies: <This should only be executed once. At startup.>
useEffect(() => { // useEffect(() => {
log.debug("UserAuthenticationContext | App Startup | loading saved user data."); // log.debug("UserAuthenticationContext | App Startup | loading saved user data.");
(async () => { // (async () => {
try { // try {
// await loadAssetsAsync(); // // await loadAssetsAsync();
await cacheAssetsAsync({ // await cacheAssetsAsync({
images: [ // images: [
"../assets/background_default.png", // "../assets/background_default.png",
"../assets/beasy_default.png", // "../assets/beasy_default.png",
"../assets/beasy_background.png", // "../assets/beasy_background.png",
"../assets/background_content_white2.png", // "../assets/background_content_white2.png",
"../../assets/background.png", // "../../assets/background.png",
], // ],
}); // });
const authenticationData = await loadAuthenticationData(); // const authenticationData = await loadAuthenticationData();
const userInformations = await loadUserInformations(); // const userInformations = await loadUserInformations();
if (authenticationData && userInformations) { // if (authenticationData && userInformations) {
setAuthenticationData(authenticationData); // setAuthenticationData(authenticationData);
setUserInformations(userInformations); // setUserInformations(userInformations);
setIsAuthenticated(true); // setIsAuthenticated(true);
} // }
} catch (error) { // } catch (error) {
log.error( // log.error(
"UserAuthenticationContext | App Startup | error during retrieval of stored data |", // "UserAuthenticationContext | App Startup | error during retrieval of stored data |",
JSON.stringify(error, null, 2), // JSON.stringify(error, null, 2),
); // );
} finally { // } finally {
setTimeout(async () => await SplashScreen.hideAsync(), 500); // setTimeout(async () => await SplashScreen.hideAsync(), 500);
} // }
})(); // })();
}, []); // }, []);
return ( return (
<UserAuthenticationContext.Provider <UserAuthenticationContext.Provider
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment