DocSpace-client/packages/common/components/PrivateRoute/index.js

173 lines
3.7 KiB
JavaScript
Raw Normal View History

2021-03-18 18:54:03 +00:00
/* eslint-disable react/prop-types */
import React from "react";
import { Navigate, Route, useLocation } from "react-router-dom";
2021-03-18 18:54:03 +00:00
import { inject, observer } from "mobx-react";
import AppLoader from "../AppLoader";
2021-03-22 14:35:33 +00:00
import combineUrl from "../../utils/combineUrl";
import { TenantStatus } from "../../constants";
2021-03-18 18:54:03 +00:00
const PrivateRoute = ({ children, ...rest }) => {
2021-03-18 18:54:03 +00:00
const {
isAdmin,
isAuthenticated,
isLoaded,
restricted,
2021-03-18 18:54:03 +00:00
user,
2021-03-18 18:54:03 +00:00
wizardCompleted,
2022-03-24 17:56:51 +00:00
tenantStatus,
2022-09-14 06:55:22 +00:00
isNotPaidPeriod,
withManager,
2023-02-22 13:52:48 +00:00
withCollaborator,
isLogout,
2021-03-18 18:54:03 +00:00
} = rest;
const location = useLocation();
const renderComponent = () => {
const isPortalUrl = location.pathname === "/preparation-portal";
2021-03-18 18:54:03 +00:00
const isPaymentsUrl =
location.pathname === "/portal-settings/payments/portal-payments";
const isBackupUrl =
location.pathname === "/portal-settings/backup/data-backup";
const isPortalUnavailableUrl = location.pathname === "/portal-unavailable";
const isPortalDeletionUrl =
location.pathname === "/portal-settings/delete-data/deletion" ||
location.pathname === "/portal-settings/delete-data/deactivation";
2021-03-18 18:54:03 +00:00
if (isLoaded && !isAuthenticated) {
// console.log("PrivateRoute render Redirect to login", rest);
const redirectPath = wizardCompleted ? "/login" : "/wizard";
2022-12-23 10:56:42 +00:00
const isHomeUrl = location.pathname === "/";
if (wizardCompleted && !isHomeUrl && !isLogout) {
2022-12-23 10:56:42 +00:00
sessionStorage.setItem("referenceUrl", window.location.href);
}
2023-04-06 14:02:10 +00:00
return <Navigate replace to={redirectPath} />;
2021-03-18 18:54:03 +00:00
}
if (
isLoaded &&
((!isNotPaidPeriod && isPortalUnavailableUrl) ||
(!user.isOwner && isPortalDeletionUrl))
) {
2023-04-06 14:02:10 +00:00
return <Navigate replace to={"/"} />;
}
2022-03-24 17:56:51 +00:00
if (
isLoaded &&
isAuthenticated &&
tenantStatus === TenantStatus.PortalRestore &&
!isPortalUrl
2022-03-24 17:56:51 +00:00
) {
return (
2023-04-06 14:02:10 +00:00
<Navigate
replace
to={combineUrl(
window.DocSpaceConfig?.proxy?.url,
"/preparation-portal"
)}
2022-03-24 17:56:51 +00:00
/>
);
}
if (
2022-09-14 06:55:22 +00:00
isNotPaidPeriod &&
isLoaded &&
(user.isOwner || user.isAdmin) &&
!isPaymentsUrl &&
!isBackupUrl &&
!isPortalDeletionUrl
) {
return (
2023-04-06 14:02:10 +00:00
<Navigate
replace
to={combineUrl(
window.DocSpaceConfig?.proxy?.url,
"/portal-settings/payments/portal-payments"
)}
/>
);
}
if (
2022-09-14 06:55:22 +00:00
isNotPaidPeriod &&
isLoaded &&
!user.isOwner &&
!user.isAdmin &&
!isPortalUnavailableUrl
) {
return (
2023-04-06 14:02:10 +00:00
<Navigate
replace
to={combineUrl(
window.DocSpaceConfig?.proxy?.url,
"/portal-unavailable"
)}
/>
);
}
// if (!isLoaded) {
// return <AppLoader />;
2021-03-18 18:54:03 +00:00
// }
if (
!restricted ||
isAdmin ||
2023-02-22 13:52:48 +00:00
(withManager && !user.isVisitor && !user.isCollaborator) ||
(withCollaborator && !user.isVisitor)
2021-03-18 18:54:03 +00:00
) {
return children;
2021-03-18 18:54:03 +00:00
}
if (restricted) {
2023-04-06 14:02:10 +00:00
return <Navigate replace to={"/error401"} />;
2021-03-18 18:54:03 +00:00
}
2023-04-06 14:02:10 +00:00
return <Navigate replace to={"/error404"} />;
2021-03-18 18:54:03 +00:00
};
const component = renderComponent();
return component;
2021-03-18 18:54:03 +00:00
};
export default inject(({ auth }) => {
const {
userStore,
isAuthenticated,
isLoaded,
isAdmin,
settingsStore,
2022-09-14 06:55:22 +00:00
currentTariffStatusStore,
isLogout,
} = auth;
2022-09-14 06:55:22 +00:00
const { isNotPaidPeriod } = currentTariffStatusStore;
2021-03-18 18:54:03 +00:00
const { user } = userStore;
const { wizardCompleted, tenantStatus } = settingsStore;
2021-03-18 18:54:03 +00:00
return {
2022-09-14 06:55:22 +00:00
isNotPaidPeriod,
2021-03-18 18:54:03 +00:00
user,
isAuthenticated,
isAdmin,
isLoaded,
2021-03-18 18:54:03 +00:00
wizardCompleted,
2022-03-24 17:56:51 +00:00
tenantStatus,
isLogout,
2021-03-18 18:54:03 +00:00
};
})(observer(PrivateRoute));