Problemas de login
This commit is contained in:
parent
924225d246
commit
2746f8ec23
@ -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: <Navigate to='/quotes' replace={true} />,
|
||||
element: <ProtectedRoute />,
|
||||
},
|
||||
];
|
||||
|
||||
|
||||
@ -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 <LoadingOverlay />;
|
||||
@ -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 <Navigate to={redirectTo} state={{ from: location }} replace />;
|
||||
}
|
||||
|
||||
|
||||
@ -8,6 +8,8 @@ export const createAxiosAuthActions = (
|
||||
httpClient = createAxiosInstance()
|
||||
): IAuthActions => ({
|
||||
login: async ({ email, password }: ILogin_DTO) => {
|
||||
secureLocalStorage.clear();
|
||||
|
||||
try {
|
||||
const result = await httpClient.request<ILogin_Response_DTO>({
|
||||
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,
|
||||
}
|
||||
|
||||
@ -18,7 +18,7 @@ export const useLogin = (params?: UseMutationOptions<AuthActionResponse, Error,
|
||||
mutationFn: login,
|
||||
onSuccess: (data, variables, context) => {
|
||||
const { success, redirectTo } = data;
|
||||
if (success && redirectTo) {
|
||||
if (success) {
|
||||
navigate(redirectTo || "/quotes");
|
||||
}
|
||||
if (onSuccess) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user