2024-04-30 11:34:49 +00:00
|
|
|
import { FC, ReactNode } from "react";
|
2024-05-01 13:42:17 +00:00
|
|
|
import { observer } from "mobx-react";
|
2024-05-01 09:24:35 +00:00
|
|
|
import { useRouter } from "next/router";
|
|
|
|
import useSWR from "swr";
|
|
|
|
import { Spinner } from "@plane/ui";
|
|
|
|
// helpers
|
|
|
|
import { EPageTypes } from "@/helpers/authentication.helper";
|
2024-05-01 06:43:36 +00:00
|
|
|
// hooks
|
2024-05-02 10:34:14 +00:00
|
|
|
import { useUser, useUserProfile, useUserSettings, useWorkspace } from "@/hooks/store";
|
2024-04-30 11:34:49 +00:00
|
|
|
|
2024-05-01 09:24:35 +00:00
|
|
|
type TPageType = EPageTypes;
|
2024-04-30 11:34:49 +00:00
|
|
|
|
|
|
|
type TAuthenticationWrapper = {
|
|
|
|
children: ReactNode;
|
2024-05-01 09:24:35 +00:00
|
|
|
pageType?: TPageType;
|
|
|
|
};
|
|
|
|
|
|
|
|
const isValidURL = (url: string): boolean => {
|
|
|
|
const disallowedSchemes = /^(https?|ftp):\/\//i;
|
|
|
|
return !disallowedSchemes.test(url);
|
2024-04-30 11:34:49 +00:00
|
|
|
};
|
|
|
|
|
2024-05-01 13:42:17 +00:00
|
|
|
export const AuthenticationWrapper: FC<TAuthenticationWrapper> = observer((props) => {
|
2024-05-01 09:24:35 +00:00
|
|
|
const router = useRouter();
|
|
|
|
const { next_path } = router.query;
|
|
|
|
// props
|
|
|
|
const { children, pageType = EPageTypes.AUTHENTICATED } = props;
|
2024-05-01 06:43:36 +00:00
|
|
|
// hooks
|
2024-05-02 10:34:14 +00:00
|
|
|
const { isLoading: isUserLoading, data: currentUser, fetchCurrentUser } = useUser();
|
|
|
|
const { isLoading: currentUserProfileLoader, data: currentUserProfile, fetchUserProfile } = useUserProfile();
|
2024-05-01 09:24:35 +00:00
|
|
|
const {
|
2024-05-02 10:34:14 +00:00
|
|
|
isLoading: currentUserSettingsLoader,
|
|
|
|
data: currentUserSettings,
|
|
|
|
fetchCurrentUserSettings,
|
|
|
|
} = useUserSettings();
|
2024-05-01 09:24:35 +00:00
|
|
|
const { loader: workspaceLoader, workspaces, fetchWorkspaces } = useWorkspace();
|
|
|
|
|
2024-05-02 09:22:18 +00:00
|
|
|
useSWR("USER_INFORMATION", async () => await fetchCurrentUser(), {
|
2024-05-01 13:28:01 +00:00
|
|
|
revalidateOnFocus: false,
|
|
|
|
shouldRetryOnError: false,
|
|
|
|
});
|
|
|
|
|
2024-05-01 09:24:35 +00:00
|
|
|
useSWR(
|
2024-05-01 13:28:01 +00:00
|
|
|
currentUser && currentUser?.id ? "USER_PROFILE_SETTINGS_INFORMATION" : null,
|
2024-05-01 09:24:35 +00:00
|
|
|
async () => {
|
2024-05-01 13:42:17 +00:00
|
|
|
if (currentUser && currentUser?.id) {
|
2024-05-02 09:22:18 +00:00
|
|
|
await fetchCurrentUserSettings();
|
|
|
|
await fetchUserProfile();
|
|
|
|
await fetchWorkspaces();
|
2024-05-01 09:24:35 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{ revalidateOnFocus: false, shouldRetryOnError: false }
|
|
|
|
);
|
|
|
|
|
|
|
|
const getWorkspaceRedirectionUrl = (): string => {
|
|
|
|
let redirectionRoute = "/profile";
|
|
|
|
|
|
|
|
// validating the next_path from the router query
|
|
|
|
if (next_path && isValidURL(next_path.toString())) {
|
|
|
|
redirectionRoute = next_path.toString();
|
|
|
|
return redirectionRoute;
|
|
|
|
}
|
|
|
|
|
|
|
|
// validate the last and fallback workspace_slug
|
|
|
|
const currentWorkspaceSlug =
|
|
|
|
currentUserSettings?.workspace?.last_workspace_slug || currentUserSettings?.workspace?.fallback_workspace_slug;
|
|
|
|
|
|
|
|
// validate the current workspace_slug is available in the user's workspace list
|
|
|
|
const isCurrentWorkspaceValid = Object.values(workspaces || {}).findIndex(
|
|
|
|
(workspace) => workspace.slug === currentWorkspaceSlug
|
|
|
|
);
|
|
|
|
|
|
|
|
if (isCurrentWorkspaceValid >= 0) redirectionRoute = `/${currentWorkspaceSlug}`;
|
|
|
|
|
|
|
|
return redirectionRoute;
|
|
|
|
};
|
|
|
|
|
2024-05-02 10:34:14 +00:00
|
|
|
if (isUserLoading || currentUserProfileLoader || currentUserSettingsLoader || workspaceLoader)
|
2024-05-01 09:24:35 +00:00
|
|
|
return (
|
|
|
|
<div className="relative flex h-screen w-full items-center justify-center">
|
|
|
|
<Spinner />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
|
|
|
|
if (pageType === EPageTypes.PUBLIC) return <>{children}</>;
|
|
|
|
|
|
|
|
if (pageType === EPageTypes.NON_AUTHENTICATED) {
|
2024-05-01 13:28:01 +00:00
|
|
|
if (!currentUser?.id) return <>{children}</>;
|
2024-05-01 09:24:35 +00:00
|
|
|
else {
|
|
|
|
if (currentUserProfile?.is_onboarded) {
|
|
|
|
const currentRedirectRoute = getWorkspaceRedirectionUrl();
|
|
|
|
router.push(currentRedirectRoute);
|
2024-05-02 08:37:33 +00:00
|
|
|
return <></>;
|
2024-05-01 09:24:35 +00:00
|
|
|
} else {
|
|
|
|
router.push("/onboarding");
|
2024-05-02 08:37:33 +00:00
|
|
|
return <></>;
|
2024-05-01 09:24:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pageType === EPageTypes.ONBOARDING) {
|
2024-05-01 13:28:01 +00:00
|
|
|
if (!currentUser?.id) {
|
2024-05-01 09:24:35 +00:00
|
|
|
router.push("/accounts/sign-in");
|
2024-05-02 08:37:33 +00:00
|
|
|
return <></>;
|
2024-05-01 09:24:35 +00:00
|
|
|
} else {
|
|
|
|
if (currentUser && currentUserProfile?.is_onboarded) {
|
|
|
|
const currentRedirectRoute = getWorkspaceRedirectionUrl();
|
|
|
|
router.push(currentRedirectRoute);
|
2024-05-02 08:37:33 +00:00
|
|
|
return <></>;
|
2024-05-01 09:24:35 +00:00
|
|
|
} else return <>{children}</>;
|
|
|
|
}
|
|
|
|
}
|
2024-05-01 06:43:36 +00:00
|
|
|
|
2024-05-01 09:24:35 +00:00
|
|
|
if (pageType === EPageTypes.AUTHENTICATED) {
|
2024-05-01 13:28:01 +00:00
|
|
|
if (currentUser?.id) {
|
2024-05-01 13:53:28 +00:00
|
|
|
if (currentUserProfile?.is_onboarded) return <>{children}</>;
|
|
|
|
else {
|
|
|
|
router.push(`/onboarding`);
|
2024-05-02 08:37:33 +00:00
|
|
|
return <></>;
|
2024-05-01 09:24:35 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
router.push("/accounts/sign-in");
|
2024-05-02 08:37:33 +00:00
|
|
|
return <></>;
|
2024-05-01 09:24:35 +00:00
|
|
|
}
|
|
|
|
}
|
2024-04-30 11:34:49 +00:00
|
|
|
|
2024-05-01 09:24:35 +00:00
|
|
|
return <>{children}</>;
|
2024-05-01 13:42:17 +00:00
|
|
|
});
|