mirror of
https://github.com/makeplane/plane
synced 2024-06-14 14:31:34 +00:00
dev: route validation on non authenticated pages (#1238)
This commit is contained in:
parent
f9cd1b1352
commit
3d5fcbd4ce
@ -5,7 +5,7 @@ import useSWR from "swr";
|
||||
// components
|
||||
import ToastAlert from "components/toast-alert";
|
||||
// hooks
|
||||
import useUser from "hooks/use-user";
|
||||
import useUserAuth from "hooks/use-user-auth";
|
||||
// services
|
||||
import projectService from "services/project.service";
|
||||
// fetch-keys
|
||||
@ -65,7 +65,7 @@ export const reducer: ReducerFunctionType = (state, action) => {
|
||||
|
||||
export const ThemeContextProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
|
||||
const [state, dispatch] = useReducer(reducer, initialState);
|
||||
const { user } = useUser();
|
||||
const { user } = useUserAuth(null);
|
||||
|
||||
const router = useRouter();
|
||||
const { workspaceSlug, projectId } = router.query;
|
||||
|
@ -22,7 +22,9 @@ const useUserAuth = (routeAuth: "sign-in" | "onboarding" | "admin" | null = "adm
|
||||
isLoading,
|
||||
error,
|
||||
mutate,
|
||||
} = useSWR<ICurrentUserResponse>(CURRENT_USER, () => userService.currentUser());
|
||||
} = useSWR<ICurrentUserResponse>(CURRENT_USER, () => userService.currentUser(), {
|
||||
refreshInterval: 0,
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
const handleWorkSpaceRedirection = async () => {
|
||||
@ -80,18 +82,23 @@ const useUserAuth = (routeAuth: "sign-in" | "onboarding" | "admin" | null = "adm
|
||||
}
|
||||
};
|
||||
|
||||
if (!isLoading) {
|
||||
setIsRouteAccess(() => true);
|
||||
if (user) {
|
||||
if (next_url) router.push(next_url);
|
||||
else handleUserRouteAuthentication();
|
||||
} else {
|
||||
if (routeAuth === "sign-in") {
|
||||
setIsRouteAccess(() => false);
|
||||
return;
|
||||
if (routeAuth === null) {
|
||||
setIsRouteAccess(() => false);
|
||||
return;
|
||||
} else {
|
||||
if (!isLoading) {
|
||||
setIsRouteAccess(() => true);
|
||||
if (user) {
|
||||
if (next_url) router.push(next_url);
|
||||
else handleUserRouteAuthentication();
|
||||
} else {
|
||||
router.push("/");
|
||||
return;
|
||||
if (routeAuth === "sign-in") {
|
||||
setIsRouteAccess(() => false);
|
||||
return;
|
||||
} else {
|
||||
router.push("/");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -30,12 +30,12 @@ import { WORKSPACE_INVITATION } from "constants/fetch-keys";
|
||||
const WorkspaceInvitation: NextPage = () => {
|
||||
const router = useRouter();
|
||||
|
||||
const { invitationId, email } = router.query;
|
||||
const { invitation_id, email } = router.query;
|
||||
|
||||
const { user } = useUser();
|
||||
|
||||
const { data: invitationDetail, error } = useSWR(invitationId && WORKSPACE_INVITATION, () =>
|
||||
invitationId ? workspaceService.getWorkspaceInvitation(invitationId as string) : null
|
||||
const { data: invitationDetail, error } = useSWR(invitation_id && WORKSPACE_INVITATION, () =>
|
||||
invitation_id ? workspaceService.getWorkspaceInvitation(invitation_id as string) : null
|
||||
);
|
||||
|
||||
const handleAccept = () => {
|
@ -2,15 +2,25 @@ import axios from "axios";
|
||||
import Cookies from "js-cookie";
|
||||
|
||||
const unAuthorizedStatus = [401];
|
||||
const nonValidatedRoutes = ["/", "/reset-password", "/workspace-member-invitation"];
|
||||
|
||||
const validateRouteCheck = (route: string): boolean => {
|
||||
let validationToggle = false;
|
||||
const routeCheck = nonValidatedRoutes.find((_route: string) => _route === route);
|
||||
if (routeCheck) validationToggle = true;
|
||||
return validationToggle;
|
||||
};
|
||||
|
||||
axios.interceptors.response.use(
|
||||
(response) => response,
|
||||
(error) => {
|
||||
const { status }: any = error.response;
|
||||
if (unAuthorizedStatus.includes(status)) {
|
||||
Cookies.remove("refreshToken", { path: "/" });
|
||||
Cookies.remove("accessToken", { path: "/" });
|
||||
if (window.location.pathname != "/")
|
||||
if (!validateRouteCheck(window.location.pathname)) {
|
||||
if (unAuthorizedStatus.includes(status)) {
|
||||
Cookies.remove("refreshToken", { path: "/" });
|
||||
Cookies.remove("accessToken", { path: "/" });
|
||||
window.location.href = `/?next_url=${window.location.pathname}`;
|
||||
}
|
||||
}
|
||||
return Promise.reject(error);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user