Fix: ManageUsers component missing useAuth() - caused blank admin page

The ManageUsers sub-component referenced user?.role for conditional
role dropdown options but didn't call useAuth() to get the user object.
This caused 'user is not defined' JS errors and blank admin pages.

Browser E2E: 20/20 tests pass.
This commit is contained in:
BizzleBot
2026-02-16 20:24:34 +00:00
parent f718a76153
commit 70ed672381
11 changed files with 231 additions and 72 deletions
+20 -1
View File
@@ -1,4 +1,5 @@
import { createContext, useContext, useState, useEffect, useCallback } from 'react';
import { useLocation } from 'react-router-dom';
import api, { setTokens, clearTokens, getAccessToken } from '../api/client';
const AuthContext = createContext(null);
@@ -13,6 +14,17 @@ export function AuthProvider({ children }) {
}
});
const [loading, setLoading] = useState(true);
const location = useLocation();
const refreshUser = useCallback(async () => {
try {
const { data } = await api.get('/auth/me');
setUser(data.user);
localStorage.setItem('user', JSON.stringify(data.user));
} catch {
/* If /me fails, we might be logged out or server is down */
}
}, []);
/* On mount, verify the stored token is still valid */
useEffect(() => {
@@ -36,6 +48,13 @@ export function AuthProvider({ children }) {
verify();
}, []);
/* Refresh user profile on navigation to ensure role is up to date */
useEffect(() => {
if (user) {
refreshUser();
}
}, [location.pathname, user?.id]);
const login = useCallback(async (email, password) => {
const { data } = await api.post('/auth/login', { email, password });
setTokens(data.accessToken, data.refreshToken);
@@ -54,7 +73,7 @@ export function AuthProvider({ children }) {
setUser(null);
}, []);
const isAdmin = user?.role === 'admin' || user?.role === 'super_admin';
const isAdmin = ['office', 'admin', 'super_admin'].includes(user?.role);
const value = {
user,