@asgerami/zemenay-blog
Version:
Plug-and-play blog system for Next.js - Get a fully functional blog running in minutes with zero configuration
72 lines (71 loc) • 4.29 kB
JavaScript
;
"use client";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AdminRoute = AdminRoute;
exports.AdminLayout = AdminLayout;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const supabase_1 = require("../lib/supabase");
function AdminRoute({ children, fallback, className = "", }) {
const [user, setUser] = (0, react_1.useState)(null);
const [loading, setLoading] = (0, react_1.useState)(true);
const [error, setError] = (0, react_1.useState)(null);
(0, react_1.useEffect)(() => {
checkAuth();
}, []);
const checkAuth = async () => {
try {
const supabase = (0, supabase_1.getSupabaseClient)();
// Get current user
const { data: { user }, error: userError, } = await supabase.auth.getUser();
if (userError) {
setError("Authentication error");
setLoading(false);
return;
}
if (!user) {
setError("Not authenticated");
setLoading(false);
return;
}
setUser(user);
setError(null);
}
catch (err) {
setError("Failed to check authentication");
}
finally {
setLoading(false);
}
};
const signIn = async () => {
try {
const supabase = (0, supabase_1.getSupabaseClient)();
const { error } = await supabase.auth.signInWithOAuth({
provider: "google",
options: {
redirectTo: `${window.location.origin}/admin`,
},
});
if (error) {
setError("Failed to sign in");
}
}
catch (err) {
setError("Sign in error");
}
};
if (loading) {
return ((0, jsx_runtime_1.jsx)("div", { className: `admin-route-loading ${className}`, children: (0, jsx_runtime_1.jsxs)("div", { className: "loading-container", children: [(0, jsx_runtime_1.jsx)("div", { className: "loading-spinner" }), (0, jsx_runtime_1.jsx)("p", { children: "Checking authentication..." })] }) }));
}
if (error || !user) {
if (fallback) {
return (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: fallback });
}
return ((0, jsx_runtime_1.jsx)("div", { className: `admin-route-error ${className}`, children: (0, jsx_runtime_1.jsx)("div", { className: "auth-container", children: (0, jsx_runtime_1.jsxs)("div", { className: "auth-card", children: [(0, jsx_runtime_1.jsx)("h2", { children: "\uD83D\uDD12 Admin Access Required" }), (0, jsx_runtime_1.jsx)("p", { children: "You need to be signed in as an admin to access this page." }), error && ((0, jsx_runtime_1.jsx)("div", { className: "error-message", children: (0, jsx_runtime_1.jsxs)("p", { children: ["Error: ", error] }) })), (0, jsx_runtime_1.jsxs)("div", { className: "auth-actions", children: [(0, jsx_runtime_1.jsx)("button", { onClick: signIn, className: "sign-in-btn", children: "Sign In with Google" }), (0, jsx_runtime_1.jsx)("a", { href: "/", className: "back-home-btn", children: "\u2190 Back to Blog" })] })] }) }) }));
}
return (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: children });
}
function AdminLayout({ children, title = "Admin Panel", description, className = "", }) {
return ((0, jsx_runtime_1.jsx)(AdminRoute, { children: (0, jsx_runtime_1.jsxs)("div", { className: `admin-layout ${className}`, children: [(0, jsx_runtime_1.jsxs)("div", { className: "admin-breadcrumb", children: [(0, jsx_runtime_1.jsx)("a", { href: "/", children: "Blog" }), " / ", (0, jsx_runtime_1.jsx)("a", { href: "/admin", children: "Admin" }), title !== "Admin Panel" && ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [" ", "/ ", (0, jsx_runtime_1.jsx)("span", { children: title })] }))] }), (title || description) && ((0, jsx_runtime_1.jsxs)("div", { className: "admin-page-header", children: [title && (0, jsx_runtime_1.jsx)("h1", { children: title }), description && (0, jsx_runtime_1.jsx)("p", { children: description })] })), (0, jsx_runtime_1.jsx)("div", { className: "admin-content", children: children })] }) }));
}