forked from github/plane
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,6 +82,10 @@ const useUserAuth = (routeAuth: "sign-in" | "onboarding" | "admin" | null = "adm
|
||||
}
|
||||
};
|
||||
|
||||
if (routeAuth === null) {
|
||||
setIsRouteAccess(() => false);
|
||||
return;
|
||||
} else {
|
||||
if (!isLoading) {
|
||||
setIsRouteAccess(() => true);
|
||||
if (user) {
|
||||
@ -95,6 +101,7 @@ const useUserAuth = (routeAuth: "sign-in" | "onboarding" | "admin" | null = "adm
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}, [user, isLoading, routeAuth, router, next_url]);
|
||||
|
||||
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,16 +2,26 @@ 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 (!validateRouteCheck(window.location.pathname)) {
|
||||
if (unAuthorizedStatus.includes(status)) {
|
||||
Cookies.remove("refreshToken", { path: "/" });
|
||||
Cookies.remove("accessToken", { path: "/" });
|
||||
if (window.location.pathname != "/")
|
||||
window.location.href = `/?next_url=${window.location.pathname}`;
|
||||
}
|
||||
}
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
|
Loading…
Reference in New Issue
Block a user