diff --git a/src/frontend/src/App.tsx b/src/frontend/src/App.tsx index 9e66d383d..20f2a18f9 100644 --- a/src/frontend/src/App.tsx +++ b/src/frontend/src/App.tsx @@ -16,10 +16,8 @@ import { FETCH_ERROR_MESSAGE, } from "./constants/constants"; import { alertContext } from "./contexts/alertContext"; -import { AuthContext } from "./contexts/authContext"; import { locationContext } from "./contexts/locationContext"; import { TabsContext } from "./contexts/tabsContext"; -import { autoLogin, getLoggedUser } from "./controllers/API"; import { typesContext } from "./contexts/typesContext"; import Router from "./routes"; @@ -46,7 +44,7 @@ export default function App() { setSuccessOpen, setErrorData, loading, - setLoading + setLoading, } = useContext(alertContext); const navigate = useNavigate(); const { fetchError } = useContext(typesContext); @@ -60,11 +58,6 @@ export default function App() { }> >([]); - const isLoginPage = location.pathname.includes("login"); - const isAdminPage = location.pathname.includes("admin"); - const isSignUpPage = location.pathname.includes("signup"); - const isLocalHost = window.location.href.includes("localhost"); - // Use effect hook to update alertsList when a new alert is added useEffect(() => { // If there is an error alert open with data, add it to the alertsList @@ -140,39 +133,6 @@ export default function App() { ); }; - //this function is to get the user logged in when the page is refreshed - const { setUserData, getAuthentication, login, setAutoLogin, logout, setIsAdmin } = - useContext(AuthContext); - - useEffect(() => { - setTimeout(() => { - autoLogin().then((user) => { - if(user && user['access_token']){ - user['refresh_token'] = "auto"; - login(user['access_token'], user['refresh_token']); - setUserData(user); - setAutoLogin(true); - setLoading(false); - } - }).catch((error) => { - setAutoLogin(false); - if (getAuthentication() && !isLoginPage) { - getLoggedUser() - .then((user) => { - setUserData(user); - setLoading(false); - const isSuperUser = user.is_superuser; - setIsAdmin(isSuperUser); - }) - .catch((error) => {}); - } - else{ - setLoading(false); - } - }); - }, 500); - }, []); - return ( //need parent component with width and height
diff --git a/src/frontend/src/contexts/authContext.tsx b/src/frontend/src/contexts/authContext.tsx index c8abb3aad..c1c06d1eb 100644 --- a/src/frontend/src/contexts/authContext.tsx +++ b/src/frontend/src/contexts/authContext.tsx @@ -1,8 +1,9 @@ -import { createContext, useEffect, useState } from "react"; +import { createContext, useContext, useEffect, useState } from "react"; import Cookies from "universal-cookie"; -import { getLoggedUser, getRepoStars } from "../controllers/API"; +import { autoLogin as autoLoginApi, getLoggedUser } from "../controllers/API"; import { Users } from "../types/api"; import { AuthContextType } from "../types/contexts/auth"; +import { alertContext } from "./alertContext"; const initialValue: AuthContextType = { isAdmin: false, @@ -25,13 +26,17 @@ export const AuthContext = createContext(initialValue); export function AuthProvider({ children }): React.ReactElement { const cookies = new Cookies(); - const [accessToken, setAccessToken] = useState(cookies.get("access_token")); - const [refreshToken, setRefreshToken] = useState(cookies.get("refresh_token")); + const [accessToken, setAccessToken] = useState( + cookies.get("access_token") + ); + const [refreshToken, setRefreshToken] = useState( + cookies.get("refresh_token") + ); const [isAuthenticated, setIsAuthenticated] = useState(false); const [isAdmin, setIsAdmin] = useState(false); const [userData, setUserData] = useState(null); const [autoLogin, setAutoLogin] = useState(false); - + const { setLoading } = useContext(alertContext); useEffect(() => { const storedAccessToken = cookies.get("access_token"); if (storedAccessToken) { @@ -39,6 +44,35 @@ export function AuthProvider({ children }): React.ReactElement { } }, []); + useEffect(() => { + const isLoginPage = location.pathname.includes("login"); + + autoLoginApi() + .then((user) => { + if (user && user["access_token"]) { + user["refresh_token"] = "auto"; + login(user["access_token"], user["refresh_token"]); + setUserData(user); + setAutoLogin(true); + setLoading(false); + } + }) + .catch((error) => { + setAutoLogin(false); + if (getAuthentication() && !isLoginPage) { + getLoggedUser() + .then((user) => { + setUserData(user); + setLoading(false); + const isSuperUser = user.is_superuser; + setIsAdmin(isSuperUser); + }) + .catch((error) => {}); + } else { + setLoading(false); + } + }); + }, []); function getAuthentication() { const storedRefreshToken = cookies.get("refresh_token"); diff --git a/src/frontend/src/contexts/index.tsx b/src/frontend/src/contexts/index.tsx index b213ace9d..f90cb7812 100644 --- a/src/frontend/src/contexts/index.tsx +++ b/src/frontend/src/contexts/index.tsx @@ -1,6 +1,8 @@ import { ReactNode } from "react"; +import { BrowserRouter } from "react-router-dom"; import { ReactFlowProvider } from "reactflow"; import { TooltipProvider } from "../components/ui/tooltip"; +import { ApiInterceptor } from "../controllers/API/api"; import { SSEProvider } from "./SSEContext"; import { AlertProvider } from "./alertContext"; import { AuthProvider } from "./authContext"; @@ -9,34 +11,32 @@ import { LocationProvider } from "./locationContext"; import { TabsProvider } from "./tabsContext"; import { TypesProvider } from "./typesContext"; import { UndoRedoProvider } from "./undoRedoContext"; -import { BrowserRouter } from "react-router-dom"; -import { ApiInterceptor } from "../controllers/API/api"; export default function ContextWrapper({ children }: { children: ReactNode }) { //element to wrap all context return ( <> - - - - - - - - - - - - {children} - - - - - - - - - + + + + + + + + + + + + {children} + + + + + + + + + ); diff --git a/src/frontend/src/contexts/tabsContext.tsx b/src/frontend/src/contexts/tabsContext.tsx index e6fdf13dc..ae3e8f160 100644 --- a/src/frontend/src/contexts/tabsContext.tsx +++ b/src/frontend/src/contexts/tabsContext.tsx @@ -129,6 +129,7 @@ export function TabsProvider({ children }: { children: ReactNode }) { } useEffect(() => { + // If the user is authenticated, fetch the types. This code is important to check if the user is auth because of the execution order of the useEffect hooks. if (getAuthentication() === true) { // get data from db //get tabs locally saved diff --git a/src/frontend/src/contexts/typesContext.tsx b/src/frontend/src/contexts/typesContext.tsx index 2e02c2b7a..5521e8ebf 100644 --- a/src/frontend/src/contexts/typesContext.tsx +++ b/src/frontend/src/contexts/typesContext.tsx @@ -41,6 +41,7 @@ export function TypesProvider({ children }: { children: ReactNode }) { const { getAuthentication } = useContext(AuthContext); useEffect(() => { + // If the user is authenticated, fetch the types. This code is important to check if the user is auth because of the execution order of the useEffect hooks. if (getAuthentication() === true) { getTypes(); } diff --git a/src/frontend/src/routes.tsx b/src/frontend/src/routes.tsx index 9b1d35b55..cd8f86c9a 100644 --- a/src/frontend/src/routes.tsx +++ b/src/frontend/src/routes.tsx @@ -106,9 +106,9 @@ const Router = () => { + - + } >