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:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user