From 2746f8ec23ad0a3644a3e66f63558e9f031cc19a Mon Sep 17 00:00:00 2001 From: David Arranz Date: Thu, 14 Nov 2024 19:50:26 +0100 Subject: [PATCH] Problemas de login --- client/src/Routes.tsx | 4 ++-- .../src/components/ProtectedRoute/ProtectedRoute.tsx | 11 +++++------ client/src/lib/axios/createAxiosAuthActions.ts | 8 ++++++-- client/src/lib/hooks/useAuth/useLogin.tsx | 2 +- 4 files changed, 14 insertions(+), 11 deletions(-) diff --git a/client/src/Routes.tsx b/client/src/Routes.tsx index e2f7f04..a812f92 100644 --- a/client/src/Routes.tsx +++ b/client/src/Routes.tsx @@ -1,4 +1,4 @@ -import { Navigate, Outlet, RouterProvider, createBrowserRouter } from "react-router-dom"; +import { Outlet, RouterProvider, createBrowserRouter } from "react-router-dom"; import { DealerLayout, DealersList, @@ -21,7 +21,7 @@ export const Routes = () => { const routesForPublic = [ { path: "/", - element: , + element: , }, ]; diff --git a/client/src/components/ProtectedRoute/ProtectedRoute.tsx b/client/src/components/ProtectedRoute/ProtectedRoute.tsx index a4bd944..712e29d 100644 --- a/client/src/components/ProtectedRoute/ProtectedRoute.tsx +++ b/client/src/components/ProtectedRoute/ProtectedRoute.tsx @@ -1,7 +1,6 @@ import { ErrorOverlay, LoadingOverlay } from "@/components"; import { useGetProfile, useIsLoggedIn } from "@/lib/hooks"; import React, { useId } from "react"; -import { useTranslation } from "react-i18next"; import { Navigate, useLocation } from "react-router-dom"; type ProctectRouteProps = { @@ -10,7 +9,7 @@ type ProctectRouteProps = { export const ProtectedRoute = ({ children }: ProctectRouteProps) => { const location = useLocation(); - const { i18n } = useTranslation(); + //const { i18n } = useTranslation(); const id = useId(); const { @@ -25,15 +24,15 @@ export const ProtectedRoute = ({ children }: ProctectRouteProps) => { error: profileError, data: profile, } = useGetProfile({ - enabled: isLoggedInSuccess, + enabled: isLoggedInSuccess && authenticated, }); // Cambiamos el idioma de la aplicación si es necesario - React.useEffect(() => { + /*React.useEffect(() => { if (isProfileSuccess && profile?.lang_code && i18n.language !== profile.lang_code) { i18n.changeLanguage(profile.lang_code); } - }, [isProfileSuccess, profile?.lang_code, i18n]); + }, [isProfileSuccess, profile?.lang_code, i18n]);*/ if (isLoggedInLoading || isProfileLoading) { return ; @@ -45,7 +44,7 @@ export const ProtectedRoute = ({ children }: ProctectRouteProps) => { } // Redirección si el usuario no está autenticado - if (isLoggedInSuccess && !authenticated) { + if ((isLoggedInSuccess && !authenticated) || (isProfileSuccess && !profile?.id)) { return ; } diff --git a/client/src/lib/axios/createAxiosAuthActions.ts b/client/src/lib/axios/createAxiosAuthActions.ts index ac32658..f0f9053 100644 --- a/client/src/lib/axios/createAxiosAuthActions.ts +++ b/client/src/lib/axios/createAxiosAuthActions.ts @@ -8,6 +8,8 @@ export const createAxiosAuthActions = ( httpClient = createAxiosInstance() ): IAuthActions => ({ login: async ({ email, password }: ILogin_DTO) => { + secureLocalStorage.clear(); + try { const result = await httpClient.request({ url: `${apiUrl}/auth/login`, @@ -24,7 +26,6 @@ export const createAxiosAuthActions = ( return { success: true, data, - redirectTo: "/quotes", }; } catch (error) { return { @@ -48,9 +49,12 @@ export const createAxiosAuthActions = ( check: () => { const authUser = secureLocalStorage.getItem("uecko.auth") as ILogin_Response_DTO; + const isAuthenticated = !!authUser?.token; + + if (!isAuthenticated) secureLocalStorage.clear(); return Promise.resolve( - authUser?.token + isAuthenticated ? { authenticated: true, } diff --git a/client/src/lib/hooks/useAuth/useLogin.tsx b/client/src/lib/hooks/useAuth/useLogin.tsx index ed1b336..54ab781 100644 --- a/client/src/lib/hooks/useAuth/useLogin.tsx +++ b/client/src/lib/hooks/useAuth/useLogin.tsx @@ -18,7 +18,7 @@ export const useLogin = (params?: UseMutationOptions { const { success, redirectTo } = data; - if (success && redirectTo) { + if (success) { navigate(redirectTo || "/quotes"); } if (onSuccess) {