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 { LOG } from "@logger";
import AsyncStorage from "@react-native-async-storage/async-storage";
// import { type NavigationProp, useNavigation } from "@react-navigation/native";
import { useMutation } from "@tanstack/react-query";
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 type { IauthenticationData } from "./Types";
......@@ -69,7 +68,7 @@ export const UserAuthenticationContextProvider = ({ children }: { children: Reac
access: "",
refresh: "",
});
const [error, setError] = useState("");
const [_error, setError] = useState("");
const [userInformations, setUserInformations] = useState<IuserInformations>({
username: "JohnDoe",
email: "JohnDoe@example.com",
......@@ -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({
mutationFn: (userAccessToken: string) => getUserInformations(userAccessToken),
onMutate: () => {
......@@ -187,7 +195,7 @@ export const UserAuthenticationContextProvider = ({ children }: { children: Reac
});
await clearStorages();
})();
}, []);
}, [clearStorages]);
// Storages
......@@ -209,59 +217,50 @@ export const UserAuthenticationContextProvider = ({ children }: { children: Reac
}
};
const clearStorages = async () => {
try {
await AsyncStorage.clear();
} catch (error) {
log.error("clearStorages |", JSON.stringify(error, null, 2));
// saving error
}
};
const loadAuthenticationData = async () => {
const _loadAuthenticationData = async () => {
log.debug("loadAuthenticationData | Loading authentication data");
const jsonRepresentation = await AsyncStorage.getItem("authenticationData");
return jsonRepresentation ? JSON.parse(jsonRepresentation) : null;
};
const loadUserInformations = async () => {
const _loadUserInformations = async () => {
log.debug("loadUserInformations | Loading user informations");
const jsonRepresentation = await AsyncStorage.getItem("userInformations");
return jsonRepresentation ? JSON.parse(jsonRepresentation) : null;
};
// biome-ignore lint/correctness/useExhaustiveDependencies: <This should only be executed once. At startup.>
useEffect(() => {
log.debug("UserAuthenticationContext | App Startup | loading saved user data.");
(async () => {
try {
// await loadAssetsAsync();
await cacheAssetsAsync({
images: [
"../assets/background_default.png",
"../assets/beasy_default.png",
"../assets/beasy_background.png",
"../assets/background_content_white2.png",
"../../assets/background.png",
],
});
const authenticationData = await loadAuthenticationData();
const userInformations = await loadUserInformations();
if (authenticationData && userInformations) {
setAuthenticationData(authenticationData);
setUserInformations(userInformations);
setIsAuthenticated(true);
}
} catch (error) {
log.error(
"UserAuthenticationContext | App Startup | error during retrieval of stored data |",
JSON.stringify(error, null, 2),
);
} finally {
setTimeout(async () => await SplashScreen.hideAsync(), 500);
}
})();
}, []);
// useEffect(() => {
// log.debug("UserAuthenticationContext | App Startup | loading saved user data.");
// (async () => {
// try {
// // await loadAssetsAsync();
// await cacheAssetsAsync({
// images: [
// "../assets/background_default.png",
// "../assets/beasy_default.png",
// "../assets/beasy_background.png",
// "../assets/background_content_white2.png",
// "../../assets/background.png",
// ],
// });
// const authenticationData = await loadAuthenticationData();
// const userInformations = await loadUserInformations();
// if (authenticationData && userInformations) {
// setAuthenticationData(authenticationData);
// setUserInformations(userInformations);
// setIsAuthenticated(true);
// }
// } catch (error) {
// log.error(
// "UserAuthenticationContext | App Startup | error during retrieval of stored data |",
// JSON.stringify(error, null, 2),
// );
// } finally {
// setTimeout(async () => await SplashScreen.hideAsync(), 500);
// }
// })();
// }, []);
return (
<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