@asgardeo/react-router
Version:
React Router integration for Asgardeo React SDK with protected routes.
53 lines (50 loc) • 1.91 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var index_exports = {};
__export(index_exports, {
ProtectedRoute: () => ProtectedRoute_default
});
module.exports = __toCommonJS(index_exports);
// src/components/ProtectedRoute.tsx
var import_react_router = require("react-router");
var import_react = require("@asgardeo/react");
var import_jsx_runtime = require("react/jsx-runtime");
var ProtectedRoute = ({ children, fallback, redirectTo, loader = null }) => {
const { isSignedIn, isLoading } = (0, import_react.useAsgardeo)();
if (isLoading) {
return loader;
}
if (isSignedIn) {
return children;
}
if (fallback) {
return fallback;
}
if (redirectTo) {
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_router.Navigate, { to: redirectTo, replace: true });
}
throw new import_react.AsgardeoRuntimeError(
'"fallback" or "redirectTo" prop is required.',
"ProtectedRoute-ValidationError-001",
"react-router",
'Either "fallback" or "redirectTo" prop must be provided to handle unauthenticated users.'
);
};
var ProtectedRoute_default = ProtectedRoute;
//# sourceMappingURL=index.js.map