Admin Dashboard
Learn how to manage admin access and view system statistics
The admin dashboard provides system-wide statistics and user management capabilities. Only designated admin users can access this protected area.
Prerequisites
- Admin access configured in
.env.local - Database connection established
- Understanding of email-based access control
Overview
The admin system provides:
- Statistics Dashboard: View total users, revenue, and plan distribution
- Admin Management: Add or remove admin access for team members
- Email-Based Access: Simple and secure access control using email addresses
- Bootstrap Admin: Set initial admin via environment variable
Setting Up Admin Access
Configure Bootstrap Admin Email
The first admin (bootstrap admin) is set via environment variable. This admin can then grant access to other team members.
1# Admin Access2ADMIN_EMAIL="your-email@example.com"Bootstrap Admin
ADMIN_EMAIL will automatically have admin access and will be added to the database on first login.Access the Admin Dashboard
Once configured, log in with your admin email and navigate to:
1/app/admin/dashboardYou'll see system-wide statistics including:
- Total user count
- Total revenue (calculated from active subscriptions)
- Plan distribution breakdown (Free, Pro, Enterprise)
Add Additional Admins
From the Admin Dashboard, click on "Admin Management" to add more admin users:
- Navigate to
/app/admin/settings - Enter the email address of the person you want to grant admin access
- Click "Add Admin"
- The email will be added to the database immediately
Security Note
How Admin Access Works
Two-Layer Access Control
The system checks admin status using two layers:
- Environment Variable (Bootstrap): Checks if email matches
ADMIN_EMAIL - Database Records: Checks if email exists in the
Admintable
Admin Utility Functions
The codebase provides utility functions in src/lib/admin.ts:
1// Check if an email has admin access2export async function isAdmin(email: string): Promise<boolean>3 4// Get all admin emails from database5export async function getAdminEmails(): Promise<string[]>6 7// Validate email format8export function isValidEmail(email: string): booleanDatabase Schema
1model Admin {2 id String @id @default(cuid())3 email String @unique4 addedAt DateTime @default(now())5 addedBy String?6 7 @@index([email])8}Admin API Routes
Available Endpoints
Admin-only API routes are located in src/app/api/admin/:
GET /api/admin/stats
Retrieves system-wide statistics (users, revenue, plan breakdown)
1const response = await fetch('/api/admin/stats');2const stats = await response.json();3// { totalUsers, totalRevenue, planBreakdown }GET /api/admin/admins
Lists all admin email addresses
1const response = await fetch('/api/admin/admins');2const { admins } = await response.json();3// admins: string[]POST /api/admin/admins
Add a new admin email
1const response = await fetch('/api/admin/admins', {2 method: 'POST',3 headers: { 'Content-Type': 'application/json' },4 body: JSON.stringify({ email: 'newadmin@example.com' })5});DELETE /api/admin/admins
Remove an admin email
1const response = await fetch('/api/admin/admins', {2 method: 'DELETE',3 headers: { 'Content-Type': 'application/json' },4 body: JSON.stringify({ email: 'oldadmin@example.com' })5});Permission Checks
Troubleshooting
Can't Access Admin Dashboard
- Verify your email matches the
ADMIN_EMAILin.env.local - Ensure you're logged in with the admin email account
- Check that the database connection is working
- Restart the dev server after changing environment variables
Admin Added But Can't Access
- Email addresses are case-insensitive and trimmed automatically
- Admin must log in with the exact email that was added
- Check the database
Admintable to verify the email exists
Statistics Not Showing
- Verify the database has subscription data
- Check browser console for API errors
- Ensure
/api/admin/statsendpoint is accessible
Customization
Add Custom Statistics
Extend the stats calculation in src/lib/stats.ts:
1export async function getAdminStats() {2 // Add your custom metrics here3 const customMetric = await prisma.yourModel.count();4 5 return {6 totalUsers,7 totalRevenue,8 planBreakdown,9 customMetric, // Add your metric10 };11}Customize Dashboard Components
Admin UI components are located in src/components/admin/:
StatCard.tsx- Statistic display cardsAdminManagement.tsx- Admin email management interface
Related Guides
Next Steps
Now that you understand the admin system, you might want to:
- Learn about Billing Setup to understand revenue tracking
- Explore User Management for user operations
- Review Database Schema for data structure