From d320ddd30aff22b418325434fee86320a12d1c4e Mon Sep 17 00:00:00 2001 From: Cristhian Zanforlin Lousa Date: Wed, 16 Aug 2023 08:38:49 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(App.tsx):=20add=20autoLogin?= =?UTF-8?q?=20function=20to=20handle=20automatic=20login=20on=20page=20ref?= =?UTF-8?q?resh=20=F0=9F=90=9B=20fix(authLoginGuard/index.tsx):=20change?= =?UTF-8?q?=20condition=20to=20check=20autoLogin=20flag=20instead=20of=20L?= =?UTF-8?q?OCALHOST=5FJWT=20flag=20=F0=9F=90=9B=20fix(headerComponent/inde?= =?UTF-8?q?x.tsx):=20change=20condition=20to=20check=20autoLogin=20flag=20?= =?UTF-8?q?instead=20of=20LOCALHOST=5FJWT=20flag=20=F0=9F=90=9B=20fix(cons?= =?UTF-8?q?tants.ts):=20remove=20unused=20LOCALHOST=5FJWT=20variable=20?= =?UTF-8?q?=E2=9C=A8=20feat(authContext.tsx):=20add=20autoLogin=20and=20se?= =?UTF-8?q?tAutoLogin=20functions=20to=20AuthContext=20=F0=9F=90=9B=20fix(?= =?UTF-8?q?API/api.tsx):=20add=20auto=5Flogin=20endpoint=20to=20the=20list?= =?UTF-8?q?=20of=20excluded=20endpoints=20from=20Authorization=20header=20?= =?UTF-8?q?=E2=9C=A8=20feat(API/index.ts):=20add=20autoLogin=20function=20?= =?UTF-8?q?to=20handle=20automatic=20login=20=F0=9F=90=9B=20fix(AdminPage/?= =?UTF-8?q?index.tsx):=20change=20handleDisableUser=20function=20parameter?= =?UTF-8?q?=20from=20user.is=5Fsuperuser=20to=20user.is=5Factive=20?= =?UTF-8?q?=F0=9F=90=9B=20fix(types/contexts/auth.ts):=20add=20autoLogin?= =?UTF-8?q?=20and=20setAutoLogin=20to=20AuthContextType?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/frontend/src/App.tsx | 68 +++++++------------ .../components/PaginatorComponent/index.tsx | 12 ---- .../src/components/authLoginGuard/index.tsx | 7 +- .../src/components/headerComponent/index.tsx | 7 +- src/frontend/src/constants/constants.ts | 2 - src/frontend/src/contexts/authContext.tsx | 5 ++ src/frontend/src/controllers/API/api.tsx | 4 ++ src/frontend/src/controllers/API/index.ts | 15 ++++ src/frontend/src/pages/AdminPage/index.tsx | 4 +- src/frontend/src/types/contexts/auth.ts | 2 + 10 files changed, 56 insertions(+), 70 deletions(-) diff --git a/src/frontend/src/App.tsx b/src/frontend/src/App.tsx index 6404daf1e..99901e3a0 100644 --- a/src/frontend/src/App.tsx +++ b/src/frontend/src/App.tsx @@ -14,10 +14,8 @@ import { alertContext } from "./contexts/alertContext"; import { AuthContext } from "./contexts/authContext"; import { locationContext } from "./contexts/locationContext"; import { TabsContext } from "./contexts/tabsContext"; -import { getLoggedUser, onLogin } from "./controllers/API"; +import { autoLogin, getLoggedUser, onLogin } from "./controllers/API"; import Router from "./routes"; -import { LOCALHOST_JWT } from "./constants/constants"; -import { LoginType } from "./types/api"; export default function App() { let { setCurrent, setShowSideBar, setIsStackedOpen } = @@ -133,52 +131,32 @@ export default function App() { }; //this function is to get the user logged in when the page is refreshed - const { setUserData, getAuthentication, login } = useContext(AuthContext); + const { setUserData, getAuthentication, login, setAutoLogin } = useContext(AuthContext); + useEffect(() => { setTimeout(() => { - if (getAuthentication && !isLoginPage) { - getLoggedUser() - .then((user) => { - setUserData(user); - }) - .catch((error) => {}); - } - }, 1000); + autoLogin().then((user) => { + if(user && user['access_token']){ + user['refresh_token'] = "auto"; + login(user['access_token'], user['refresh_token']); + setAutoLogin(true); + } + }).catch((error) => { + setAutoLogin(false); + if (getAuthentication && !isLoginPage) { + getLoggedUser() + .then((user) => { + setUserData(user); + }) + .catch((error) => {}); + } + else{ + navigate("/login"); + } + }); + }, 500); }, []); - useEffect(() => { - - if(LOCALHOST_JWT === true && isLocalHost === true){ - const user: LoginType = { - username: "superuser", - password: "12345", - }; - onLogin(user) - .then((user) => { - login(user.access_token, user.refresh_token); - getUser(); - navigate("/"); - }) - .catch((error) => { - setErrorData({ - title: "Error signing in", - list: [error["response"]["data"]["detail"]], - }); - }); - } - }, []) - - function getUser() { - if (getAuthentication) { - setTimeout(() => { - getLoggedUser() - .then((user) => { - setUserData(user); - }) - .catch((error) => {}); - }, 1000); - } - } return ( //need parent component with width and height diff --git a/src/frontend/src/components/PaginatorComponent/index.tsx b/src/frontend/src/components/PaginatorComponent/index.tsx index 295b4d7c8..0bb40fa60 100644 --- a/src/frontend/src/components/PaginatorComponent/index.tsx +++ b/src/frontend/src/components/PaginatorComponent/index.tsx @@ -65,10 +65,6 @@ export default function PaginatorComponent({ Go to first page - - - - - - - - - - - -