@asgardeo/react-router
Version:
React Router integration for Asgardeo React SDK with protected routes.
60 lines (59 loc) • 1.65 kB
JavaScript
// src/components/ProtectedRoute.tsx
import { Navigate } from "react-router";
import { useAsgardeo, AsgardeoRuntimeError, navigate } from "@asgardeo/react";
import { jsx } from "react/jsx-runtime";
var ProtectedRoute = ({
children,
fallback,
redirectTo,
loader = null,
onSignIn,
signInOptions: overriddenSignInOptions = {}
}) => {
const { isSignedIn, isLoading, signIn, signInOptions, signInUrl } = useAsgardeo();
if (isLoading) {
return loader;
}
if (isSignedIn) {
return children;
}
if (fallback) {
return fallback;
}
if (redirectTo) {
return /* @__PURE__ */ jsx(Navigate, { to: redirectTo, replace: true });
}
if (!isSignedIn) {
if (signInUrl) {
navigate(signInUrl);
} else {
if (onSignIn) {
onSignIn(signIn, overriddenSignInOptions);
} else {
(async () => {
try {
await signIn(overriddenSignInOptions ?? signInOptions);
} catch (error) {
throw new AsgardeoRuntimeError(
"Sign-in failed in ProtectedRoute.",
"ProtectedRoute-SignInError-001",
"react-router",
`An error occurred during sign-in: ${error.message}`
);
}
})();
}
}
}
throw new AsgardeoRuntimeError(
"ProtectedRoute misconfiguration.",
"ProtectedRoute-Misconfiguration-001",
"react-router",
"The internal handler failed to process the state. Please try with a fallback or redirectTo prop."
);
};
var ProtectedRoute_default = ProtectedRoute;
export {
ProtectedRoute_default as ProtectedRoute
};
//# sourceMappingURL=index.js.map