import { auth } from "@/auth"; import { prisma } from "@/lib/prisma"; import { redirect } from "next/navigation"; import { Container, Typography, Paper, Table, TableBody, TableCell, TableHead, TableRow, Box, Alert, Breadcrumbs } from "@mui/material"; import Link from "next/link"; import UserRow from "./user-row"; import SettingsIcon from '@mui/icons-material/Settings'; import NavigateNextIcon from '@mui/icons-material/NavigateNext'; export default async function SettingsPage() { const session = await auth(); const userEmail = session?.user?.email || ""; /** * 1. Access Control * Check if the logged-in user is the Bootstrap Admin from .env * or has the ADMIN role assigned in the database. */ const isBootstrap = userEmail === process.env.INITIAL_ADMIN_EMAIL; const dbUser = await prisma.user.findUnique({ where: { email: userEmail }, select: { id: true, role: true } }); const isAdmin = isBootstrap || dbUser?.role === "ADMIN"; if (!isAdmin) { redirect("/dashboard"); // Unauthorized users are sent back to Dashboard } /** * 2. Data Fetching * Get all users to display in the management table. */ const allUsers = await prisma.user.findMany({ orderBy: { email: 'asc' } }); return ( {/* Breadcrumbs for easier navigation */} } aria-label="breadcrumb" sx={{ mb: 3 }} > Dashboard Settings User Management Assign Administrative privileges and manage user access. {/* 3. Bootstrap Warning Box */} {isBootstrap && ( You are authenticated via INITIAL_ADMIN_EMAIL. This provides permanent access to this page regardless of database settings. )} {/* 4. User Management Table */} User Identity Current Role Actions {allUsers.length > 0 ? ( allUsers.map((user) => ( )) ) : ( No users found in database. )}
Note: Users must log out and log back in for role changes to take effect in their active session.
); }