Skip to content

bug: Replace non-existent 'SuperAdmin' localStorage key with 'role' in EventManagement and Requests #7202

@coderabbitai

Description

@coderabbitai

Problem Description

Multiple components are checking for a 'SuperAdmin' key in localStorage that does not exist in production and has never been set during the authentication flow. This bug causes incorrect permission checks and feature access issues.

Discovery

This issue was discovered during investigation in PR #7169 by @adityai0 while fixing Organization Settings bugs.

Root Cause Analysis

The Non-Existent Key

The 'SuperAdmin' localStorage key is never set during login:

// LoginPage.tsx - handleLoginSuccess (lines 123-129)
// ❌ 'SuperAdmin' is NEVER set here
setItem('IsLoggedIn', 'TRUE');
setItem('name', user.name);
setItem('email', user.emailAddress);
setItem('role', user.role);  // ← Only 'role' is set, not 'SuperAdmin'
setItem('UserImage', sanitizeAvatarURL(user.avatarURL));
// ... NO setItem('SuperAdmin', ...) call!

Where It's Used Incorrectly

The 'SuperAdmin' key only exists in test mocks (.spec.tsx files), but production code incorrectly tries to read it:

// ❌ WRONG - This key doesn't exist!
const superAdmin = getItem('SuperAdmin');

This means:

  • Permission checks always fail
  • Features meant for admins are broken
  • getItem('SuperAdmin') always returns null in production

Files Affected

1. src/screens/AdminPortal/EventManagement/EventManagement.tsx

Current buggy code:

const superAdmin = getItem('SuperAdmin');
const userRole = superAdmin ? 'SUPERADMIN' : getItem('role');

Issue: Since superAdmin is always null, this logic never grants SUPERADMIN status.


2. src/screens/AdminPortal/Requests/Requests.tsx

Current buggy code:

const rawSuperAdmin = getItem('SuperAdmin');
const isSuperAdmin = 
  rawSuperAdmin === true || 
  rawSuperAdmin === 'true' || 
  rawSuperAdmin === 'TRUE';

Issue: rawSuperAdmin is always null, so isSuperAdmin is always false.

Note: This file later correctly checks getItem('role') at line 112, showing the correct pattern is already known in the codebase:

// ✅ CORRECT pattern (already used later in the same file!)
const userRole = getItem('role') as string;
const isAdmin = userRole?.toLowerCase() === 'administrator';

Correct Implementation Pattern

Option 1: Check role in localStorage (already set during login)

// ✅ CORRECT - Works in production
const userRole = getItem('role') as string;
const isSuperAdmin = userRole === 'administrator' || userRole === 'superuser';

Option 2: Defensive with array check

// ✅ CORRECT - Case-insensitive and safe
const userRole = getItem('role') as string;
const isSuperAdmin = Boolean(userRole && ['administrator', 'superuser'].includes(userRole.toLowerCase()));

Option 3: Query GraphQL if real-time data needed

// ✅ CORRECT - Use if fresh data from backend is required
const { data } = useQuery(CURRENT_USER);
const isSuperAdmin = data?.currentUser?.appUserProfile?.isSuperAdmin;

Acceptance Criteria

  • Replace getItem('SuperAdmin') with getItem('role') in EventManagement.tsx
  • Replace getItem('SuperAdmin') with getItem('role') in Requests.tsx
  • Use role values: 'administrator' or 'superuser' for admin checks
  • Update associated test files to remove SuperAdmin mocks and use 'role' instead
  • Verify admin features work correctly after changes
  • Add defensive null checks for userRole
  • Ensure consistency: use the same pattern across both files

Test Cases to Verify

  1. Login as Administrator

    • Verify getItem('role') returns 'administrator'
    • Verify admin-only features are accessible
  2. Login as Regular User

    • Verify getItem('role') returns 'user' or other non-admin role
    • Verify admin features are correctly hidden/disabled
  3. Unit Tests

    • Update mocks to set role: 'administrator' instead of SuperAdmin: true
    • Verify all test assertions still pass

Additional Context

Related PR

Evidence from Codebase

  • The 'SuperAdmin' key appears only in test mocks (.spec.tsx files)
  • No production code ever calls setItem('SuperAdmin', ...)
  • The 'role' field is reliably set during login and is the source of truth for user permissions

Files to Update

Production Files

  1. src/screens/AdminPortal/EventManagement/EventManagement.tsx
  2. src/screens/AdminPortal/Requests/Requests.tsx

Test Files (update mocks)

  1. src/screens/AdminPortal/EventManagement/EventManagement.spec.tsx
  2. src/screens/AdminPortal/Requests/Requests.spec.tsx

Effort Estimate

⏱️ Small - Straightforward find-and-replace with test updates


Discovered by: @adityai0 in PR #7169
References:


Note: @adityai0 - Please self-assign this issue to track the work!

Metadata

Metadata

Assignees

Labels

bugSomething isn't workinggood first issueGood for newcomerstestTesting application

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions