UNPKG

@kinde-oss/kinde-auth-react

Version:

Kinde React SDK for authentication

80 lines (79 loc) 2.45 kB
import { jsx as e, Fragment as R, jsxs as p } from "react/jsx-runtime"; import { useState as i, useEffect as m } from "react"; import { u as y } from "./useKindeAuth-BArWbaqf.js"; import { P as x } from "./index-7XdJNziI.js"; function b({ children: r }) { return /* @__PURE__ */ p( "div", { style: { padding: "20px", textAlign: "center", color: "#666", fontFamily: "system-ui, -apple-system, sans-serif" }, children: [ /* @__PURE__ */ e("h3", { children: "Protected Route" }), /* @__PURE__ */ e("p", { children: "This component requires react-router-dom to be installed." }), /* @__PURE__ */ p("p", { children: [ "Please install it: ", /* @__PURE__ */ e("code", { children: "npm install react-router-dom" }) ] }), /* @__PURE__ */ e( "div", { style: { marginTop: "20px", padding: "10px", border: "1px solid #ddd", borderRadius: "4px" }, children: r } ) ] } ); } function F({ children: r, has: n, fallbackPath: a = "/" }) { const { isLoading: h, isAuthenticated: s } = y(), [g, l] = i(!1), [d, c] = i(null), [o, v] = i( null ), [u, f] = i( null ); return m(() => { (async () => { try { const { Navigate: t } = await import("react-router-dom"); v(() => t), f(!0); } catch { console.warn( "react-router-dom is not available. ProtectedRoute will render a fallback." ), f(!1); } })(); }, []), m(() => { s && (async () => { if (!n) { c(!0); return; } l(!0); try { const t = await x(n); c(t); } catch (t) { console.error("Access check failed:", t), c(!1); } finally { l(!1); } })(); }, [n, s]), u === null || h || g ? /* @__PURE__ */ e("div", { children: "Loading..." }) : u === !1 ? /* @__PURE__ */ e(b, { children: r }) : s ? d === !1 ? o ? /* @__PURE__ */ e(o, { to: a, replace: !0 }) : /* @__PURE__ */ e("div", { children: "Redirecting..." }) : d === null ? /* @__PURE__ */ e("div", { children: "Loading..." }) : /* @__PURE__ */ e(R, { children: r }) : o ? /* @__PURE__ */ e(o, { to: a, replace: !0 }) : /* @__PURE__ */ e("div", { children: "Redirecting..." }); } export { F as ProtectedRoute };