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 {
|
import {
|
||||||
DealerLayout,
|
DealerLayout,
|
||||||
DealersList,
|
DealersList,
|
||||||
@ -21,7 +21,7 @@ export const Routes = () => {
|
|||||||
const routesForPublic = [
|
const routesForPublic = [
|
||||||
{
|
{
|
||||||
path: "/",
|
path: "/",
|
||||||
element: <Navigate to='/quotes' replace={true} />,
|
element: <ProtectedRoute />,
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|||||||
@ -1,7 +1,6 @@
|
|||||||
import { ErrorOverlay, LoadingOverlay } from "@/components";
|
import { ErrorOverlay, LoadingOverlay } from "@/components";
|
||||||
import { useGetProfile, useIsLoggedIn } from "@/lib/hooks";
|
import { useGetProfile, useIsLoggedIn } from "@/lib/hooks";
|
||||||
import React, { useId } from "react";
|
import React, { useId } from "react";
|
||||||
import { useTranslation } from "react-i18next";
|
|
||||||
import { Navigate, useLocation } from "react-router-dom";
|
import { Navigate, useLocation } from "react-router-dom";
|
||||||
|
|
||||||
type ProctectRouteProps = {
|
type ProctectRouteProps = {
|
||||||
@ -10,7 +9,7 @@ type ProctectRouteProps = {
|
|||||||
|
|
||||||
export const ProtectedRoute = ({ children }: ProctectRouteProps) => {
|
export const ProtectedRoute = ({ children }: ProctectRouteProps) => {
|
||||||
const location = useLocation();
|
const location = useLocation();
|
||||||
const { i18n } = useTranslation();
|
//const { i18n } = useTranslation();
|
||||||
const id = useId();
|
const id = useId();
|
||||||
|
|
||||||
const {
|
const {
|
||||||
@ -25,15 +24,15 @@ export const ProtectedRoute = ({ children }: ProctectRouteProps) => {
|
|||||||
error: profileError,
|
error: profileError,
|
||||||
data: profile,
|
data: profile,
|
||||||
} = useGetProfile({
|
} = useGetProfile({
|
||||||
enabled: isLoggedInSuccess,
|
enabled: isLoggedInSuccess && authenticated,
|
||||||
});
|
});
|
||||||
|
|
||||||
// Cambiamos el idioma de la aplicación si es necesario
|
// Cambiamos el idioma de la aplicación si es necesario
|
||||||
React.useEffect(() => {
|
/*React.useEffect(() => {
|
||||||
if (isProfileSuccess && profile?.lang_code && i18n.language !== profile.lang_code) {
|
if (isProfileSuccess && profile?.lang_code && i18n.language !== profile.lang_code) {
|
||||||
i18n.changeLanguage(profile.lang_code);
|
i18n.changeLanguage(profile.lang_code);
|
||||||
}
|
}
|
||||||
}, [isProfileSuccess, profile?.lang_code, i18n]);
|
}, [isProfileSuccess, profile?.lang_code, i18n]);*/
|
||||||
|
|
||||||
if (isLoggedInLoading || isProfileLoading) {
|
if (isLoggedInLoading || isProfileLoading) {
|
||||||
return <LoadingOverlay />;
|
return <LoadingOverlay />;
|
||||||
@ -45,7 +44,7 @@ export const ProtectedRoute = ({ children }: ProctectRouteProps) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Redirección si el usuario no está autenticado
|
// 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 />;
|
return <Navigate to={redirectTo} state={{ from: location }} replace />;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -8,6 +8,8 @@ export const createAxiosAuthActions = (
|
|||||||
httpClient = createAxiosInstance()
|
httpClient = createAxiosInstance()
|
||||||
): IAuthActions => ({
|
): IAuthActions => ({
|
||||||
login: async ({ email, password }: ILogin_DTO) => {
|
login: async ({ email, password }: ILogin_DTO) => {
|
||||||
|
secureLocalStorage.clear();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const result = await httpClient.request<ILogin_Response_DTO>({
|
const result = await httpClient.request<ILogin_Response_DTO>({
|
||||||
url: `${apiUrl}/auth/login`,
|
url: `${apiUrl}/auth/login`,
|
||||||
@ -24,7 +26,6 @@ export const createAxiosAuthActions = (
|
|||||||
return {
|
return {
|
||||||
success: true,
|
success: true,
|
||||||
data,
|
data,
|
||||||
redirectTo: "/quotes",
|
|
||||||
};
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return {
|
return {
|
||||||
@ -48,9 +49,12 @@ export const createAxiosAuthActions = (
|
|||||||
|
|
||||||
check: () => {
|
check: () => {
|
||||||
const authUser = secureLocalStorage.getItem("uecko.auth") as ILogin_Response_DTO;
|
const authUser = secureLocalStorage.getItem("uecko.auth") as ILogin_Response_DTO;
|
||||||
|
const isAuthenticated = !!authUser?.token;
|
||||||
|
|
||||||
|
if (!isAuthenticated) secureLocalStorage.clear();
|
||||||
|
|
||||||
return Promise.resolve(
|
return Promise.resolve(
|
||||||
authUser?.token
|
isAuthenticated
|
||||||
? {
|
? {
|
||||||
authenticated: true,
|
authenticated: true,
|
||||||
}
|
}
|
||||||
|
|||||||
@ -18,7 +18,7 @@ export const useLogin = (params?: UseMutationOptions<AuthActionResponse, Error,
|
|||||||
mutationFn: login,
|
mutationFn: login,
|
||||||
onSuccess: (data, variables, context) => {
|
onSuccess: (data, variables, context) => {
|
||||||
const { success, redirectTo } = data;
|
const { success, redirectTo } = data;
|
||||||
if (success && redirectTo) {
|
if (success) {
|
||||||
navigate(redirectTo || "/quotes");
|
navigate(redirectTo || "/quotes");
|
||||||
}
|
}
|
||||||
if (onSuccess) {
|
if (onSuccess) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user