feat: Implement admin user management page with create/delete functionality, add a super-admin-only sidebar link, and expose isSuperAdmin in AuthContext.

This commit is contained in:
pradeepkumar
2026-03-20 17:04:59 +05:30
parent 8043d66898
commit 37326db652
3 changed files with 240 additions and 1 deletions

View File

@@ -8,6 +8,7 @@ interface AuthContextType {
user: AuthUser | null;
isLoading: boolean;
isAuthenticated: boolean;
isSuperAdmin: boolean;
login: (email: string, password: string) => Promise<void>;
logout: () => Promise<void>;
error: string | null;
@@ -25,7 +26,7 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
try {
const storedUser = authService.getStoredUser();
if (storedUser && storedUser.role === 'ADMIN') {
if (storedUser && (storedUser.role === 'ADMIN' || storedUser.role === 'SUPER_ADMIN')) {
setUser(storedUser);
} else if (storedUser) {
// Not admin, clear storage
@@ -47,6 +48,13 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
setError(null);
try {
const response = await authService.login({ email, password });
// Block non-admin users from admin panel
if (response.user.role !== 'ADMIN' && response.user.role !== 'SUPER_ADMIN') {
authService.clearAuth();
throw new Error('Access denied. Only admin users can login here.');
}
setUser(response.user);
router.push('/dashboard');
} catch (err) {
@@ -71,6 +79,7 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
user,
isLoading,
isAuthenticated: !!user,
isSuperAdmin: user?.role === 'SUPER_ADMIN',
login,
logout,
error,