mirror of
https://github.com/makeplane/plane
synced 2024-06-14 14:31:34 +00:00
88ebda42ff
* dev: update python version * dev: handle magic code attempt exhausted * dev: update app, space and god mode redirection paths * fix: handled signup and signin workflow * chore: auth input error indication and autofill styling improvement * dev: add app redirection urls * dev: update redirections * chore: onboarding improvement * chore: onboarding improvement * chore: redirection issue in space resolved * chore: instance empty state added * dev: fix app, space, admin redirection in docker setitngs --------- Co-authored-by: guru_sainath <gurusainath007@gmail.com> Co-authored-by: Anmol Singh Bhatia <anmolsinghbhatia@plane.so>
131 lines
3.9 KiB
TypeScript
131 lines
3.9 KiB
TypeScript
import { FC, ReactNode } from "react";
|
|
import { observer } from "mobx-react";
|
|
import { useRouter } from "next/router";
|
|
import useSWR from "swr";
|
|
import { Spinner } from "@plane/ui";
|
|
// helpers
|
|
import { EPageTypes } from "@/helpers/authentication.helper";
|
|
// hooks
|
|
import { useUser, useUserProfile, useUserSettings, useWorkspace } from "@/hooks/store";
|
|
|
|
type TPageType = EPageTypes;
|
|
|
|
type TAuthenticationWrapper = {
|
|
children: ReactNode;
|
|
pageType?: TPageType;
|
|
};
|
|
|
|
const isValidURL = (url: string): boolean => {
|
|
const disallowedSchemes = /^(https?|ftp):\/\//i;
|
|
return !disallowedSchemes.test(url);
|
|
};
|
|
|
|
export const AuthenticationWrapper: FC<TAuthenticationWrapper> = observer((props) => {
|
|
const router = useRouter();
|
|
const { next_path } = router.query;
|
|
// props
|
|
const { children, pageType = EPageTypes.AUTHENTICATED } = props;
|
|
// hooks
|
|
const { isLoading: isUserLoading, data: currentUser, fetchCurrentUser } = useUser();
|
|
const { data: currentUserProfile } = useUserProfile();
|
|
const { data: currentUserSettings } = useUserSettings();
|
|
const { workspaces } = useWorkspace();
|
|
|
|
const { isLoading: isUserSWRLoading } = useSWR("USER_INFORMATION", async () => await fetchCurrentUser(), {
|
|
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;
|
|
};
|
|
|
|
if (isUserSWRLoading || isUserLoading)
|
|
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) {
|
|
if (!currentUser?.id) return <>{children}</>;
|
|
else {
|
|
if (currentUserProfile?.id && currentUserProfile?.is_onboarded) {
|
|
const currentRedirectRoute = getWorkspaceRedirectionUrl();
|
|
router.push(currentRedirectRoute);
|
|
return <></>;
|
|
} else {
|
|
router.push("/onboarding");
|
|
return <></>;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (pageType === EPageTypes.ONBOARDING) {
|
|
if (!currentUser?.id) {
|
|
router.push("/sign-in");
|
|
return <></>;
|
|
} else {
|
|
if (currentUser && currentUserProfile?.id && currentUserProfile?.is_onboarded) {
|
|
const currentRedirectRoute = getWorkspaceRedirectionUrl();
|
|
router.push(currentRedirectRoute);
|
|
return <></>;
|
|
} else return <>{children}</>;
|
|
}
|
|
}
|
|
|
|
if (pageType === EPageTypes.SET_PASSWORD) {
|
|
if (!currentUser?.id) {
|
|
router.push("/sign-in");
|
|
return <></>;
|
|
} else {
|
|
if (
|
|
currentUser &&
|
|
!currentUser?.is_password_autoset &&
|
|
currentUserProfile?.id &&
|
|
currentUserProfile?.is_onboarded
|
|
) {
|
|
const currentRedirectRoute = getWorkspaceRedirectionUrl();
|
|
router.push(currentRedirectRoute);
|
|
return <></>;
|
|
} else return <>{children}</>;
|
|
}
|
|
}
|
|
|
|
if (pageType === EPageTypes.AUTHENTICATED) {
|
|
if (currentUser?.id) {
|
|
if (currentUserProfile && currentUserProfile?.id && currentUserProfile?.is_onboarded) return <>{children}</>;
|
|
else {
|
|
router.push(`/onboarding`);
|
|
return <></>;
|
|
}
|
|
} else {
|
|
router.push("/sign-in");
|
|
return <></>;
|
|
}
|
|
}
|
|
|
|
return <>{children}</>;
|
|
});
|