fix(authAdminGuard): remove unnecessary promise return in logout function call
fix(authGuard): remove unnecessary promise return in logout function call fix(headerComponent): remove unnecessary promise return in logout function call fix(authContext): add navigation to login page after logout fix(api): remove unnecessary promise return in logout function call and add navigation to login page after logout
This commit is contained in:
parent
ad2351e3e7
commit
b80cad7d9d
5 changed files with 15 additions and 29 deletions
|
|
@ -7,14 +7,10 @@ export const ProtectedAdminRoute = ({ children }) => {
|
|||
useContext(AuthContext);
|
||||
|
||||
if (!isAuthenticated) {
|
||||
logout().then(() => {
|
||||
return <Navigate to="/login" replace />;
|
||||
});
|
||||
}
|
||||
|
||||
if ((userData && !isAdmin) || autoLogin) {
|
||||
logout();
|
||||
} else if ((userData && !isAdmin) || autoLogin) {
|
||||
return <Navigate to="/" replace />;
|
||||
} else {
|
||||
return children;
|
||||
}
|
||||
|
||||
return children;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,14 +1,11 @@
|
|||
import { useContext } from "react";
|
||||
import { Navigate } from "react-router-dom";
|
||||
import { AuthContext } from "../../contexts/authContext";
|
||||
|
||||
export const ProtectedRoute = ({ children }) => {
|
||||
const { isAuthenticated, logout } = useContext(AuthContext);
|
||||
if (!isAuthenticated) {
|
||||
logout().then(() => {
|
||||
return <Navigate to="/login" replace />;
|
||||
});
|
||||
logout();
|
||||
} else {
|
||||
return children;
|
||||
}
|
||||
|
||||
return children;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -193,9 +193,7 @@ export default function Header(): JSX.Element {
|
|||
<DropdownMenuItem
|
||||
className="cursor-pointer"
|
||||
onClick={() => {
|
||||
logout().then(() => {
|
||||
navigate("/login");
|
||||
});
|
||||
logout();
|
||||
}}
|
||||
>
|
||||
Sign Out
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
import { createContext, useEffect, useState } from "react";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
import Cookies from "universal-cookie";
|
||||
import {
|
||||
autoLogin as autoLoginApi,
|
||||
|
|
@ -29,6 +30,7 @@ const initialValue: AuthContextType = {
|
|||
export const AuthContext = createContext<AuthContextType>(initialValue);
|
||||
|
||||
export function AuthProvider({ children }): React.ReactElement {
|
||||
const navigate = useNavigate();
|
||||
const cookies = new Cookies();
|
||||
const [accessToken, setAccessToken] = useState<string | null>(
|
||||
cookies.get("access_token_lf") ?? null
|
||||
|
|
@ -109,6 +111,7 @@ export function AuthProvider({ children }): React.ReactElement {
|
|||
setUserData(null);
|
||||
setAccessToken(null);
|
||||
setIsAuthenticated(false);
|
||||
navigate("/login");
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
throw error;
|
||||
|
|
|
|||
|
|
@ -28,9 +28,7 @@ function ApiInterceptor() {
|
|||
authenticationErrorCount = authenticationErrorCount + 1;
|
||||
if (authenticationErrorCount > 3) {
|
||||
authenticationErrorCount = 0;
|
||||
logout().then(() => {
|
||||
navigate("/login");
|
||||
});
|
||||
logout();
|
||||
}
|
||||
try {
|
||||
const res = await renewAccessToken();
|
||||
|
|
@ -47,14 +45,10 @@ function ApiInterceptor() {
|
|||
}
|
||||
} catch (error) {
|
||||
if (axios.isAxiosError(error) && error.response?.status === 401) {
|
||||
logout().then(() => {
|
||||
navigate("/login");
|
||||
});
|
||||
logout();
|
||||
} else {
|
||||
console.error(error);
|
||||
logout().then(() => {
|
||||
navigate("/login");
|
||||
});
|
||||
logout();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -62,9 +56,7 @@ function ApiInterceptor() {
|
|||
if (!accessToken && error?.config?.url?.includes("login")) {
|
||||
return Promise.reject(error);
|
||||
} else {
|
||||
logout().then(() => {
|
||||
navigate("/login");
|
||||
});
|
||||
logout();
|
||||
}
|
||||
} else {
|
||||
// if (URL_EXCLUDED_FROM_ERROR_RETRIES.includes(error.config?.url)) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue