TorbatYar/frontend/components/AdminAuthGuard.tsx
Mortezakoohjani 800b0ba2c5 Deploy TorbatYar for torbatyar.ir with nginx multi-tenant routing.
Wire production domain, CORS for tenant subdomains, celery volume mounts, and nginx reverse proxy configs for apex, API, identity, auth, and wildcard tenants.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-21 21:43:33 +03:30

42 lines
1.4 KiB
TypeScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"use client";
import { useAuth } from "@/hooks/useAuth";
import { getStoredToken, loginWithRedirect } from "@/lib/auth";
import { useEffect } from "react";
const ADMIN_ROLES = ["platform_admin", "tenant_admin"];
/** محافظ پنل ادمین — SSO مرکزی + نقش platform_admin یا tenant_admin. */
export function AdminAuthGuard({ children }: { children: React.ReactNode }) {
const { loading, user } = useAuth();
const hasAdminRole = ADMIN_ROLES.some((r) => user?.roles?.includes(r));
useEffect(() => {
if (loading) return;
if (!getStoredToken()) {
void loginWithRedirect("/admin/tenants");
}
}, [loading]);
if (loading) {
return <p className="text-sm text-gray-500">در حال بررسی ورود...</p>;
}
if (!getStoredToken()) {
return <p className="text-sm text-gray-500">در حال هدایت به ورود مرکزی...</p>;
}
if (!hasAdminRole) {
return (
<div className="mx-auto mt-16 max-w-md rounded-2xl border border-amber-100 bg-amber-50 p-6 text-center">
<p className="font-semibold text-amber-800">دسترسی به پنل ادمین ندارید</p>
<p className="mt-2 text-sm text-amber-700">
فقط مدیر پلتفرم یا مدیر tenant میتوانند وارد این بخش شوند.
</p>
</div>
);
}
return <>{children}</>;
}