diff --git a/cosiap_frontend/src/App.jsx b/cosiap_frontend/src/App.jsx index 9c0fc17ddcb0cfbcc3aa44b23d9571de3386ed0a..ce4903609fadf6b14799f082dd1ab2f9de4c4d7a 100644 --- a/cosiap_frontend/src/App.jsx +++ b/cosiap_frontend/src/App.jsx @@ -47,11 +47,11 @@ function App() { } function RoutesApp( {setViewPageLoader} ){ - const {token} = useAutenticacion(); - + const {token, isAdmin} = useAutenticacion(); + console.log(token) return ( - } /> + } /> {/* Rutas públicas */} {/* Componente del layout */} }> @@ -74,7 +74,7 @@ function RoutesApp( {setViewPageLoader} ){ } /> } /> } /> - } /> + } /> {/* Solo administradores pueden acceder a estas url */} }> } /> diff --git a/cosiap_frontend/src/components/common/utility/Autenticador.jsx b/cosiap_frontend/src/components/common/utility/Autenticador.jsx index 22f83e33955948f5546062c197daf9dcc0c343a5..9dbe2a865a2d3271cb89115688ed562a678f5ac0 100644 --- a/cosiap_frontend/src/components/common/utility/Autenticador.jsx +++ b/cosiap_frontend/src/components/common/utility/Autenticador.jsx @@ -8,6 +8,7 @@ import { } from "react"; import api from '@/api' import PropTypes from 'prop-types'; +import { useNavigate } from "react-router-dom"; const ContextoAut = createContext(undefined); @@ -27,6 +28,7 @@ export const Autenticador = ({ setViewPageLoader, children }) => { const [isAdmin, setIsAdmin] = useState(); const [uid, setUid] = useState(); const interceptadoresRef = useRef({}); + const navigate = useNavigate(); useEffect(() => { const buscarUsuario = async () => { @@ -43,6 +45,7 @@ export const Autenticador = ({ setViewPageLoader, children }) => { } catch { setToken(null); setIsAdmin(false); + setUid(undefined); } } @@ -114,6 +117,7 @@ export const Autenticador = ({ setViewPageLoader, children }) => { setToken(null); setIsAdmin(undefined); setUid(undefined); + navigate('/authentication'); } catch (error) { console.error('Logout unsuccessful', error); } diff --git a/cosiap_frontend/src/components/common/utility/LoginRequiredRoute.jsx b/cosiap_frontend/src/components/common/utility/LoginRequiredRoute.jsx index 3ff2a8d895bfced329152b9bdd28ac3dea797583..b82f3dc4dab0d5215e56b1d6d069e99c0dccb98e 100644 --- a/cosiap_frontend/src/components/common/utility/LoginRequiredRoute.jsx +++ b/cosiap_frontend/src/components/common/utility/LoginRequiredRoute.jsx @@ -1,10 +1,12 @@ import { useAutenticacion } from "@/components/common/utility/Autenticador"; import { useState, useEffect } from "react"; -import { Navigate, Outlet } from "react-router-dom"; +import { Navigate, Outlet, useLocation } from "react-router-dom"; import api from "@/api"; export const LoginRequiredRoute = () => { + const location = useLocation(); const { token } = useAutenticacion(); + console.log(location) if (token === null) { return ; } else if (token === undefined) { @@ -31,6 +33,6 @@ export const IsLogged = () => { if (token === null) { return }else{ - return ; + return null; } }; diff --git a/cosiap_frontend/src/components/users/Login/LoginForm.jsx b/cosiap_frontend/src/components/users/Login/LoginForm.jsx index 7383d8a602e4801270dfe6f7a7f06cfd69b6a126..d2efa3ddf097f7b718e2a04308f4c3a7483daffd 100644 --- a/cosiap_frontend/src/components/users/Login/LoginForm.jsx +++ b/cosiap_frontend/src/components/users/Login/LoginForm.jsx @@ -9,12 +9,11 @@ import { LoginInputPassword } from "@/components/users/Login/LoginInputPassword" import { useState } from "react"; import {ErrorDisplay} from '@/components/common/ui/ErrorDisplay' import {useAutenticacion} from "@/components/common/utility/Autenticador" - - +import { useNavigate } from "react-router-dom"; export function LoginForm( {setViewPageLoader} ) { const [loginError, setLoginError] = useState(''); - + const navigate = useNavigate() const { setToken, setIsAdmin, configurarInterceptors, setUid } = useAutenticacion(); const { register, @@ -38,6 +37,7 @@ export function LoginForm( {setViewPageLoader} ) { //Extraemos el id const responseUid = await api.usuarios.getId(); setUid(responseUid.data.user_id); + navigate('/inicio'); } catch (error) { console.error("Login failed:", error); setLoginError(error.response.data.detail)