membros-react-sdk
Version:
React authentication library for Membros platform with subscription management and plan-based access control
45 lines • 2.15 kB
JavaScript
"use client";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useState, useEffect } from "react";
import { useAuth } from "../AuthContext.esm.js";
import { LoadingScreen } from "../components/LoadingScreen.esm.js";
import { AuthScreen } from "../components/AuthScreen.esm.js";
import { InadimplentScreen } from "../components/InadimplentScreen.esm.js";
export function withAdminAuth(Component, options) {
return function AdminAuthWrapped(props) {
const [mounted, setMounted] = useState(false);
// Ensure component only renders on client side
useEffect(() => {
setMounted(true);
}, []);
if (!mounted) {
return (_jsx("div", { className: "min-h-screen flex items-center justify-center", children: _jsxs("div", { className: "text-center", children: [_jsx("div", { className: "animate-spin rounded-full h-8 w-8 border-b-2 border-gray-900 mx-auto" }), _jsx("p", { className: "mt-2 text-sm text-gray-600", children: "Loading..." })] }) }));
}
const { user, originalUser, isLoading, adimplent } = useAuth();
const { adminUserId, useToast, LoadingComponent = LoadingScreen, AuthComponent, InadimplentComponent = InadimplentScreen, UnauthorizedComponent } = options;
if (isLoading)
return _jsx(LoadingComponent, {});
if (!user) {
if (AuthComponent) {
return _jsx(AuthComponent, {});
}
return (_jsx(AuthScreen, { useToast: useToast }));
}
if (!adimplent)
return _jsx(InadimplentComponent, {});
// Check if user is admin
const isAdmin = user.id === adminUserId || (originalUser && originalUser.id === adminUserId);
if (!isAdmin) {
if (UnauthorizedComponent) {
return _jsx(UnauthorizedComponent, {});
}
// Redirect to home
if (typeof window !== "undefined") {
window.location.href = "/";
}
return null;
}
return _jsx(Component, { ...props });
};
}
//# sourceMappingURL=withAdminAuth.js.map