fixed the loop when the token becomes invalid
This commit is contained in:
parent
2e9db61173
commit
3bd38fdefa
@ -30,5 +30,6 @@ export async function isTokenValid(token: ApiToken): Promise<boolean>{
|
|||||||
|
|
||||||
const errorCode: number = res.data.retrieveOrders.errorCode;
|
const errorCode: number = res.data.retrieveOrders.errorCode;
|
||||||
|
|
||||||
return (errorCode == API_INVALID_SHOP_ID_CODE);
|
return errorCode == API_INVALID_SHOP_ID_CODE;
|
||||||
|
// return false;
|
||||||
}
|
}
|
@ -30,6 +30,12 @@ const schema = yup
|
|||||||
export function Login() {
|
export function Login() {
|
||||||
const user = useContext(userContext);
|
const user = useContext(userContext);
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (user.token)
|
||||||
|
navigate("/");
|
||||||
|
}, [user.token])
|
||||||
|
|
||||||
const {
|
const {
|
||||||
register,
|
register,
|
||||||
formState: { errors },
|
formState: { errors },
|
||||||
@ -85,11 +91,6 @@ export function Login() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (user.token)
|
|
||||||
navigate("/");
|
|
||||||
}, [user.token])
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<main className="cardContainer">
|
<main className="cardContainer">
|
||||||
<form onSubmit={handleSubmit(onSubmit)}>
|
<form onSubmit={handleSubmit(onSubmit)}>
|
||||||
|
@ -11,15 +11,20 @@ export function AuthComponent({ children }: AuthProps) {
|
|||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const user = useContext(userContext);
|
const user = useContext(userContext);
|
||||||
|
|
||||||
async function checkIfCustomerLogged(token: ApiToken){
|
async function checkIfCustomerLogged(token: ApiToken) {
|
||||||
const isLogged: boolean = token != null && await isTokenValid(user.token);
|
const isLogged: boolean = await isTokenValid(user.token);
|
||||||
|
|
||||||
if (!isLogged){
|
if (!isLogged) {
|
||||||
navigate("/login", { replace: true });
|
user.setToken(null);
|
||||||
|
user.setId(null);
|
||||||
|
window.localStorage.removeItem("token");
|
||||||
|
window.localStorage.removeItem("customerId");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
if (!user.token)
|
||||||
|
navigate("/login", { replace: true });
|
||||||
// navigate needs to be wrapped in a useEffect so that it gets executed after the component is mounted. Otherwise it doesn't redirect
|
// navigate needs to be wrapped in a useEffect so that it gets executed after the component is mounted. Otherwise it doesn't redirect
|
||||||
checkIfCustomerLogged(user.token);
|
checkIfCustomerLogged(user.token);
|
||||||
}, [user.token])
|
}, [user.token])
|
||||||
|
Loading…
Reference in New Issue
Block a user