diff --git a/frontend/src/context/authentication/authentication.context.tsx b/frontend/src/context/authentication/authentication.context.tsx index 672a7781..a5f2d775 100644 --- a/frontend/src/context/authentication/authentication.context.tsx +++ b/frontend/src/context/authentication/authentication.context.tsx @@ -1,4 +1,3 @@ -import Loading from "components/Loading"; import React, { type ReactNode, useCallback, @@ -13,7 +12,6 @@ import { createCustomContext } from "utils"; import { postAuthLogin, postAuthRegister } from "./api"; import { - authStatus, type IAuthenticationContext, type IAuthenticationStore, } from "./authentication.interface"; @@ -30,7 +28,6 @@ export const AuthenticationProvider: React.FC<{ children: ReactNode }> = ({ children, }) => { const navigate = useNavigate(); - const [status, setStatus] = useState(authStatus.Loading); const [authLoading, setAuthLoading] = useState(false); const [store, setStore] = useState({ token: localStorage.getItem("auth_token"), @@ -58,7 +55,6 @@ export const AuthenticationProvider: React.FC<{ children: ReactNode }> = ({ "tokenExpiresAtTimestamp", tokenExpirationDate.getTime().toString(), ); - setStatus(authStatus.SignedIn); navigate(redirect); }, [navigate], @@ -71,7 +67,6 @@ export const AuthenticationProvider: React.FC<{ children: ReactNode }> = ({ ...store, token: null, })); - setStatus(authStatus.SignedOut); navigate("/sign-in"); }, [navigate]); @@ -123,9 +118,9 @@ export const AuthenticationProvider: React.FC<{ children: ReactNode }> = ({ const tokenExpired = useCallback(() => { const tokenTimestamp = localStorage.getItem("tokenExpiresAtTimestamp"); if (tokenTimestamp) { - const date1 = Number(tokenTimestamp); - const date2 = new Date().getTime(); - return date1 <= date2; + const expireDate = Number(tokenTimestamp); + const currentDate = new Date().getTime(); + return expireDate <= currentDate; } return true; }, []); @@ -146,13 +141,10 @@ export const AuthenticationProvider: React.FC<{ children: ReactNode }> = ({ useEffect(() => { const expired = tokenExpired(); - - if (expired) { + if (expired && isLogged.current) { logout(); - } else { - setStatus(authStatus.SignedIn); } - }, [tokenExpired]); + }, [tokenExpired, isLogged]); const value = useMemo((): IAuthenticationContext => { return { @@ -165,10 +157,6 @@ export const AuthenticationProvider: React.FC<{ children: ReactNode }> = ({ }; }, [store, logout, authenticate, register, authLoading]); - if (status === authStatus.Loading) { - return ; - } - return ( {children} diff --git a/frontend/src/context/authentication/authentication.interface.ts b/frontend/src/context/authentication/authentication.interface.ts index b768146f..5d8fd984 100644 --- a/frontend/src/context/authentication/authentication.interface.ts +++ b/frontend/src/context/authentication/authentication.interface.ts @@ -3,12 +3,6 @@ export interface IAuthenticationStore { userId: string | null; } -export enum authStatus { - Loading, - SignedIn, - SignedOut, -} - export interface IAuthenticationContext { store: IAuthenticationStore; isLogged: boolean;