Commit fbe2ec94 by G

logic for handling errors related to authentication

parent eb01e000
import authenticateUser from "@/utils/requests/authenticateUser"; import authenticateUser, { parseAuthicationErrors } from "@/utils/requests/authenticateUser";
import type { IuserInformations } from "@/utils/requests/types"; import type { IuserInformations } from "@/utils/requests/types";
import getUserInformations from "@/utils/requests/userInformations"; import getUserInformations from "@/utils/requests/userInformations";
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 { type NavigationProp, useNavigation } from "@react-navigation/native";
import { useMutation } from "@tanstack/react-query"; import { useMutation } from "@tanstack/react-query";
import type { AxiosError } from "axios"; import * as SplashScreen from "expo-splash-screen";
import { createContext, useCallback, useContext, useEffect, useState } from "react"; import { createContext, useCallback, useContext, useEffect, useState } from "react";
import { useModalsManagerContext } from "./ModalsManagerContext";
import type { IauthenticationData } from "./Types"; import type { IauthenticationData } from "./Types";
const log = LOG.extend("UserAuthenticationContext"); const log = LOG.extend("UserAuthenticationContext");
SplashScreen.preventAutoHideAsync();
export interface UserAuthenticationContextProps { export interface UserAuthenticationContextProps {
isAuthenticated: boolean; isAuthenticated: boolean;
setIsAuthenticated: React.Dispatch<React.SetStateAction<boolean>>; setIsAuthenticated: React.Dispatch<React.SetStateAction<boolean>>;
...@@ -86,6 +90,7 @@ export const UserAuthenticationContextProvider = ({ children }: { children: Reac ...@@ -86,6 +90,7 @@ export const UserAuthenticationContextProvider = ({ children }: { children: Reac
}); });
// Hoooks // Hoooks
const { showModal } = useModalsManagerContext();
// const navigation = useNavigation<NavigationProp<ImainStackNavigator>>(); // const navigation = useNavigation<NavigationProp<ImainStackNavigator>>();
...@@ -99,25 +104,16 @@ export const UserAuthenticationContextProvider = ({ children }: { children: Reac ...@@ -99,25 +104,16 @@ export const UserAuthenticationContextProvider = ({ children }: { children: Reac
}, },
onSuccess: (data) => { onSuccess: (data) => {
setAuthenticationData(data); setAuthenticationData(data);
// navigation.popToTop();
// navigation.replace("bottomTabs");
// navigation.navigate("bottomTabs");
log.info("Receive data from authenticateUser, running getUserInformations..."); log.info("Receive data from authenticateUser, running getUserInformations...");
userInformationsMutation.mutate(data.access); userInformationsMutation.mutate(data.access);
// console.log("user informations", userInformations);
}, },
// biome-ignore lint/suspicious/noExplicitAny: <Axios error> onError: (error: unknown) => {
onError: (error: AxiosError<any>) => { const errorString = parseAuthicationErrors(error);
log.error("authenticationMutation", error); showModal(<ErrorModal message={errorString} />);
if (error.response) { },
log.error("error :: ", error.response.data); onSettled: () => {
if (error.response.status === 400) { setIsAuthenticating(false);
return setError("Bad request");
}
const message: string = error.response.data.detail;
return setError(message);
}
setError("Unknown error");
}, },
}); });
...@@ -239,13 +235,14 @@ export const UserAuthenticationContextProvider = ({ children }: { children: Reac ...@@ -239,13 +235,14 @@ export const UserAuthenticationContextProvider = ({ children }: { children: Reac
setAuthenticationData(authenticationData); setAuthenticationData(authenticationData);
setUserInformations(userInformations); setUserInformations(userInformations);
setIsAuthenticated(true); setIsAuthenticated(true);
// navigation.navigate("appBottomTabsNavigator");
} }
} 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 {
setTimeout(async () => await SplashScreen.hideAsync(), 500);
} }
})(); })();
}, []); }, []);
......
import type { IauthenticationData } from "@/contexts/Types"; import type { IauthenticationData } from "@/contexts/Types";
import { LOG } from "@logger"; import { LOG } from "@logger";
import { AxiosError } from "axios";
import axiosRequest from "../axiosRequest"; import axiosRequest from "../axiosRequest";
const log = LOG.extend("authenticateUser"); const log = LOG.extend("authenticateUser");
...@@ -19,3 +20,18 @@ const authenticateUser = async ({ username, password }: { username: string; pass ...@@ -19,3 +20,18 @@ const authenticateUser = async ({ username, password }: { username: string; pass
}; };
export default authenticateUser; export default authenticateUser;
export const parseAuthicationErrors = (error: unknown): string => {
if (error instanceof AxiosError && error.response) {
switch (error.response.status) {
case 401:
return "Wrong username or password";
default:
return "Unknown Error. Please try again.";
}
}
if (error instanceof AxiosError && error.request) {
return "Network error";
}
return "Unknown error";
};
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