UNPKG

@asgardeo/react-router

Version:

React Router integration for Asgardeo React SDK with protected routes.

31 lines (30 loc) 933 B
// src/components/ProtectedRoute.tsx import { Navigate } from "react-router"; import { useAsgardeo, AsgardeoRuntimeError } from "@asgardeo/react"; import { jsx } from "react/jsx-runtime"; var ProtectedRoute = ({ children, fallback, redirectTo, loader = null }) => { const { isSignedIn, isLoading } = useAsgardeo(); if (isLoading) { return loader; } if (isSignedIn) { return children; } if (fallback) { return fallback; } if (redirectTo) { return /* @__PURE__ */ jsx(Navigate, { to: redirectTo, replace: true }); } throw new 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; export { ProtectedRoute_default as ProtectedRoute }; //# sourceMappingURL=index.js.map