diff --git a/apps/app/contexts/theme.context.tsx b/apps/app/contexts/theme.context.tsx index 318f04095..e8842ee59 100644 --- a/apps/app/contexts/theme.context.tsx +++ b/apps/app/contexts/theme.context.tsx @@ -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; diff --git a/apps/app/hooks/use-user-auth.tsx b/apps/app/hooks/use-user-auth.tsx index 619a2fcdd..3e92fd3fe 100644 --- a/apps/app/hooks/use-user-auth.tsx +++ b/apps/app/hooks/use-user-auth.tsx @@ -22,7 +22,9 @@ const useUserAuth = (routeAuth: "sign-in" | "onboarding" | "admin" | null = "adm isLoading, error, mutate, - } = useSWR(CURRENT_USER, () => userService.currentUser()); + } = useSWR(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; + } } } } diff --git a/apps/app/pages/workspace-member-invitation/[invitationId].tsx b/apps/app/pages/workspace-member-invitation.tsx similarity index 95% rename from apps/app/pages/workspace-member-invitation/[invitationId].tsx rename to apps/app/pages/workspace-member-invitation.tsx index 8fae9547b..b895aa90a 100644 --- a/apps/app/pages/workspace-member-invitation/[invitationId].tsx +++ b/apps/app/pages/workspace-member-invitation.tsx @@ -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 = () => { diff --git a/apps/app/services/api.service.ts b/apps/app/services/api.service.ts index cf540e2b5..b9d993548 100644 --- a/apps/app/services/api.service.ts +++ b/apps/app/services/api.service.ts @@ -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); }