@slashid/docusaurus-theme-slashid
Version:
SlashID theme for Docusaurus.
69 lines (64 loc) • 3.73 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = Root;
var _react = _interopRequireWildcard(require("react"));
var _router = require("@docusaurus/router");
var _useIsBrowser = _interopRequireDefault(require("@docusaurus/useIsBrowser"));
var _react2 = require("@slashid/react");
require("./reset.css");
require("./globals.css");
var _domain = require("../../domain");
var _useSlashIDConfig = require("../hooks/useSlashIDConfig");
var _authContext = require("./auth-context");
var _slashid = require("./slashid");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
/* ============================================================================
* Copyright (c) SlashID
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
* ========================================================================== */
const AuthCheck = ({
children
}) => {
const {
user
} = (0, _react2.useSlashID)();
const {
showLogin
} = (0, _react.useContext)(_authContext.AuthContext);
const isBrowser = (0, _useIsBrowser.default)();
const config = (0, _useSlashIDConfig.useSlashIDConfig)();
const location = (0, _router.useLocation)();
// TODO figure out where the reference to window is
if (!isBrowser) {
return null;
}
// eslint-disable-next-line testing-library/render-result-naming-convention
const shouldRenderByPath = (0, _domain.shouldPathRender)(location.pathname, config.privatePaths, user);
if (!shouldRenderByPath && config.uxMode === "redirect") {
var _config$privateRedire;
return <_router.Redirect to={(_config$privateRedire = config.privateRedirectPath) !== null && _config$privateRedire !== void 0 ? _config$privateRedire : "/"} />;
}
const shouldShowLogin = config.forceLogin && !user || showLogin || !shouldRenderByPath;
return shouldShowLogin ? <_slashid.SlashID configuration={config.formConfiguration} /> : <>{children}</>;
};
// Default implementation, that you can customize
function Root({
children
}) {
const options = (0, _useSlashIDConfig.useSlashIDConfig)();
return <_react2.SlashIDProvider oid={options.orgID} baseApiUrl={options.baseURL} sdkUrl={options.sdkURL} tokenStorage="localStorage">
<_react2.ServerThemeRoot>
<_authContext.AuthProvider>
<_react2.SlashIDLoaded>
<AuthCheck>{children}</AuthCheck>
</_react2.SlashIDLoaded>
</_authContext.AuthProvider>
</_react2.ServerThemeRoot>
</_react2.SlashIDProvider>;
}