From cf7c2152d66811c3951750a8f2036be7dedc27e1 Mon Sep 17 00:00:00 2001 From: Nathan Vieira Marcelino Date: Mon, 4 Mar 2024 12:59:12 -0300 Subject: [PATCH 1/2] chore: centralize login layout --- frontend/src/components/PrivateLayout/header/header.tsx | 5 ++--- frontend/src/components/PrivateLayout/index.tsx | 2 +- frontend/src/components/PublicLayout/index.tsx | 3 ++- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/frontend/src/components/PrivateLayout/header/header.tsx b/frontend/src/components/PrivateLayout/header/header.tsx index d96bdf06..6aa71476 100644 --- a/frontend/src/components/PrivateLayout/header/header.tsx +++ b/frontend/src/components/PrivateLayout/header/header.tsx @@ -1,15 +1,14 @@ import { Box } from "@mui/material"; -import { type FC, useRef, useState } from "react"; +import { type FC, useState } from "react"; import { DrawerMenu } from "./drawerMenu"; export const Header: FC = () => { const [menuOpen, setMenuOpen] = useState(false); - const barHeight = useRef(null); return ( <> - + { diff --git a/frontend/src/components/PrivateLayout/index.tsx b/frontend/src/components/PrivateLayout/index.tsx index 6115dcc8..0e4e3d9b 100644 --- a/frontend/src/components/PrivateLayout/index.tsx +++ b/frontend/src/components/PrivateLayout/index.tsx @@ -10,7 +10,7 @@ interface IPrivateLayoutProps { export const PrivateLayout: FC = ({ children }) => { return ( - +
diff --git a/frontend/src/components/PublicLayout/index.tsx b/frontend/src/components/PublicLayout/index.tsx index af5dc129..7cba1ca2 100644 --- a/frontend/src/components/PublicLayout/index.tsx +++ b/frontend/src/components/PublicLayout/index.tsx @@ -6,10 +6,11 @@ export const PublicLayout: FC<{ children: ReactNode }> = ({ children }) => { From bd170096749de05475ff1da3586c634205a3bf4b Mon Sep 17 00:00:00 2001 From: Nathan Vieira Marcelino Date: Mon, 4 Mar 2024 13:02:08 -0300 Subject: [PATCH 2/2] fix: token expire --- .../authentication/authentication.context.tsx | 22 +++++-------------- .../authentication.interface.ts | 6 ----- 2 files changed, 5 insertions(+), 23 deletions(-) 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;