diff --git a/src/components/common/input/Select.tsx b/src/components/common/input/Select.tsx index 2730d70..5b4c83b 100644 --- a/src/components/common/input/Select.tsx +++ b/src/components/common/input/Select.tsx @@ -9,13 +9,13 @@ interface Option { // Define the props for the CustomSelect component interface CustomSelectProps { - label: string; + label?: string; options: Option[]; placeholder?: string; width?: string; height?: string; border?: string; - labelStyle?: React.CSSProperties; // Optional style for the label + placeholderStyle?: React.CSSProperties; // Optional style for the placeholder } @@ -46,8 +46,8 @@ const CustomSelect: React.FC = ({ paddingLeft: 2, // Remove padding from the select input }} > - {options.map((option, index) => ( - ))} diff --git a/src/main.tsx b/src/main.tsx index 642e826..4b889ae 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -2,13 +2,18 @@ import { StrictMode } from "react"; import { createRoot } from "react-dom/client"; import App from "./App"; import "./components/common/i18n"; - import keycloak from "./keycloak"; import { ReactKeycloakProvider } from "@react-keycloak/web"; -createRoot(document.getElementById("root")!).render( - - - - - -); + +const rootElement = document.getElementById("root"); +if (rootElement) { + createRoot(rootElement).render( + + + + + + ); +} else { + console.error("Root element not found."); +} diff --git a/src/services/auth/asyncStorage.tsx b/src/services/auth/asyncStorage.tsx index 0237018..c56c744 100644 --- a/src/services/auth/asyncStorage.tsx +++ b/src/services/auth/asyncStorage.tsx @@ -13,7 +13,7 @@ export const saveToken = async (token: string, refreshToken: string) => { // Function to retrieve the token export const getToken = async (): Promise<{ - token: string | unknown; + token: string; refreshToken: string | null; } | null> => { try { @@ -47,9 +47,9 @@ interface JwtPayload { export const getTokenData = async (): Promise => { const tokenResponse = await getToken(); - if (tokenResponse && tokenResponse.token) { + if (tokenResponse?.token) { try { - const decoded = jwtDecode(tokenResponse.token as string); // Type assertion here + const decoded = jwtDecode(tokenResponse.token); return decoded; } catch (e) { console.error("Error decoding token:", e); diff --git a/src/services/auth/auth.tsx b/src/services/auth/auth.tsx index 79f1a0d..211d763 100644 --- a/src/services/auth/auth.tsx +++ b/src/services/auth/auth.tsx @@ -99,6 +99,9 @@ export const loginUser = async (loginData: object) => { export const logoutUser = async () => { const accessToken = localStorage.getItem("authToken"); const refreshToken = localStorage.getItem("refreshToken"); + if (!accessToken || !refreshToken) { + throw new Error("No active session found"); + } try { const response = await axios.post( `${apiBaseUrl}/auth/logout`, @@ -112,18 +115,14 @@ export const logoutUser = async () => { }, } ); - localStorage.removeItem("authToken"); - localStorage.removeItem("refreshToken"); - - return response.data; - } catch (error: unknown) { - if (axios.isAxiosError(error) && error.response) { - // Handle the error with specific type if it's an Axios error - return Promise.reject(error.response.data); - } else { - // For other types of errors (like network errors) - return Promise.reject(new Error("Network Error")); + if (response) { + localStorage.removeItem("authToken"); + localStorage.removeItem("refreshToken"); } + + return response.data as { success: boolean; message: string }; + } catch (error) { + handleError(error); } }; diff --git a/src/utils/context/checkToken.tsx b/src/utils/context/checkToken.tsx index 6dec105..b7132d9 100644 --- a/src/utils/context/checkToken.tsx +++ b/src/utils/context/checkToken.tsx @@ -1,4 +1,10 @@ -import React, { createContext, useState, useEffect, ReactNode } from "react"; +import React, { + createContext, + useState, + useEffect, + ReactNode, + useMemo, +} from "react"; import { getToken } from "../../services/auth/asyncStorage"; // Define types for the context @@ -58,7 +64,6 @@ const AuthProvider: React.FC = ({ children }) => { const [userData, setUserData] = useState(null); const [documents, setDocuments] = useState([]); - // Function to check token and update login status const checkToken = async () => { try { const token = await getToken(); @@ -88,22 +93,23 @@ const AuthProvider: React.FC = ({ children }) => { checkToken(); }, []); + const contextValue = useMemo( + () => ({ + isLoggedIn, + checkToken, + setIsLoggedIn, + userData, + documents, + updateUserData, + updateApplicationId, + applicationId, + removeContextData, + }), + [isLoggedIn, userData, documents, applicationId] // Dependencies + ); + return ( - - {children} - + {children} ); };