mirror of
https://github.com/lemeow125/Borrowing-TrackerFrontend.git
synced 2025-09-17 04:59:35 +08:00
Improved login and auth flow
This commit is contained in:
parent
99cd673b12
commit
d878cfb1aa
11 changed files with 249 additions and 99 deletions
|
@ -1,22 +1,35 @@
|
|||
import { useEffect, useState } from "react";
|
||||
import { useDispatch, useSelector } from "react-redux";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
import { useNavigate, useLocation } from "react-router-dom";
|
||||
import { JWTRefreshAPI, setAccessToken, setRefreshToken } from "../API/API";
|
||||
import { auth_toggle } from "../Plugins/Redux/Slices/AuthSlice/AuthSlice";
|
||||
import { RootState } from "../Plugins/Redux/Store/Store";
|
||||
import { toast } from "react-toastify";
|
||||
|
||||
export default function Revalidator() {
|
||||
const dispatch = useDispatch();
|
||||
const navigate = useNavigate();
|
||||
const location = useLocation();
|
||||
const authenticated = useSelector((state: RootState) => state.auth.value);
|
||||
const [rechecked, setRechecked] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
if (!authenticated && rechecked) {
|
||||
navigate("/");
|
||||
console.log("Not logged in");
|
||||
if (location.pathname !== "/") {
|
||||
navigate("/");
|
||||
toast("Please log in to continue", {
|
||||
position: "bottom-center",
|
||||
autoClose: 2000,
|
||||
hideProgressBar: false,
|
||||
closeOnClick: true,
|
||||
pauseOnHover: true,
|
||||
draggable: true,
|
||||
progress: undefined,
|
||||
theme: "light",
|
||||
});
|
||||
}
|
||||
}
|
||||
}, [authenticated, navigate, rechecked]);
|
||||
}, [authenticated, location.pathname, navigate, rechecked]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!authenticated) {
|
||||
|
@ -24,16 +37,25 @@ export default function Revalidator() {
|
|||
if (response) {
|
||||
await dispatch(auth_toggle());
|
||||
navigate("/dashboard");
|
||||
console.log("User session restored");
|
||||
toast("User session restored", {
|
||||
position: "top-right",
|
||||
autoClose: 2000,
|
||||
hideProgressBar: false,
|
||||
closeOnClick: true,
|
||||
pauseOnHover: true,
|
||||
draggable: true,
|
||||
progress: undefined,
|
||||
theme: "light",
|
||||
});
|
||||
} else {
|
||||
await setRefreshToken("");
|
||||
await setAccessToken("");
|
||||
console.log("User session expired");
|
||||
}
|
||||
setRechecked(true);
|
||||
});
|
||||
}
|
||||
}, [authenticated, dispatch, navigate]);
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, []);
|
||||
|
||||
return <></>;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue