2026-01-28 14:36:33 -07:00
|
|
|
import React, { createContext, useContext, useState, useEffect, useCallback } from 'react';
|
|
|
|
|
|
|
|
|
|
const API_BASE = process.env.REACT_APP_API_BASE || 'http://localhost:3001/api';
|
|
|
|
|
|
2026-05-05 11:04:53 -06:00
|
|
|
// Known BU teams — must match backend helpers/teams.js
|
|
|
|
|
const KNOWN_TEAMS = ['STEAM', 'ACCESS-ENG', 'ACCESS-OPS', 'INTELDEV'];
|
|
|
|
|
|
2026-01-28 14:36:33 -07:00
|
|
|
const AuthContext = createContext(null);
|
|
|
|
|
|
2026-05-05 11:31:15 -06:00
|
|
|
// Load admin scope from localStorage — returns array of selected teams
|
|
|
|
|
function loadAdminScope() {
|
|
|
|
|
try {
|
|
|
|
|
const saved = localStorage.getItem('admin_bu_scope');
|
|
|
|
|
if (saved) {
|
|
|
|
|
const parsed = JSON.parse(saved);
|
|
|
|
|
if (Array.isArray(parsed)) return parsed;
|
|
|
|
|
}
|
|
|
|
|
} catch { /* ignore */ }
|
|
|
|
|
// Default: null means "not yet initialized" — will be set to user's teams on first load
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function saveAdminScope(teams) {
|
|
|
|
|
try {
|
|
|
|
|
localStorage.setItem('admin_bu_scope', JSON.stringify(teams));
|
|
|
|
|
} catch { /* ignore */ }
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-28 14:36:33 -07:00
|
|
|
export function AuthProvider({ children }) {
|
|
|
|
|
const [user, setUser] = useState(null);
|
|
|
|
|
const [loading, setLoading] = useState(true);
|
|
|
|
|
const [error, setError] = useState(null);
|
|
|
|
|
|
2026-05-05 11:31:15 -06:00
|
|
|
// Admin scope — array of currently selected teams for filtering
|
|
|
|
|
// null = not initialized yet (will default to user's teams after login)
|
|
|
|
|
const [adminScope, setAdminScope] = useState(loadAdminScope);
|
2026-05-05 11:04:53 -06:00
|
|
|
|
2026-01-28 14:36:33 -07:00
|
|
|
// Check if user is authenticated on mount
|
|
|
|
|
const checkAuth = useCallback(async () => {
|
|
|
|
|
try {
|
|
|
|
|
const response = await fetch(`${API_BASE}/auth/me`, {
|
|
|
|
|
credentials: 'include'
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (response.ok) {
|
|
|
|
|
const data = await response.json();
|
|
|
|
|
setUser(data.user);
|
2026-05-05 11:31:15 -06:00
|
|
|
// Initialize admin scope to user's teams if not yet set
|
|
|
|
|
if (adminScope === null && data.user?.teams?.length > 0) {
|
|
|
|
|
const initial = data.user.teams;
|
|
|
|
|
setAdminScope(initial);
|
|
|
|
|
saveAdminScope(initial);
|
|
|
|
|
}
|
2026-01-28 14:36:33 -07:00
|
|
|
} else {
|
|
|
|
|
setUser(null);
|
|
|
|
|
}
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error('Auth check error:', err);
|
|
|
|
|
setUser(null);
|
|
|
|
|
} finally {
|
|
|
|
|
setLoading(false);
|
|
|
|
|
}
|
2026-05-05 11:31:15 -06:00
|
|
|
}, []); // eslint-disable-line react-hooks/exhaustive-deps
|
2026-01-28 14:36:33 -07:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
checkAuth();
|
|
|
|
|
}, [checkAuth]);
|
|
|
|
|
|
|
|
|
|
// Login function
|
|
|
|
|
const login = async (username, password) => {
|
|
|
|
|
setError(null);
|
|
|
|
|
try {
|
|
|
|
|
const response = await fetch(`${API_BASE}/auth/login`, {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: { 'Content-Type': 'application/json' },
|
|
|
|
|
credentials: 'include',
|
|
|
|
|
body: JSON.stringify({ username, password })
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const data = await response.json();
|
|
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
throw new Error(data.error || 'Login failed');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setUser(data.user);
|
2026-05-05 11:31:15 -06:00
|
|
|
// Initialize scope to user's teams on login
|
|
|
|
|
if (data.user?.teams?.length > 0 && adminScope === null) {
|
|
|
|
|
const initial = data.user.teams;
|
|
|
|
|
setAdminScope(initial);
|
|
|
|
|
saveAdminScope(initial);
|
|
|
|
|
}
|
2026-01-28 14:36:33 -07:00
|
|
|
return { success: true };
|
|
|
|
|
} catch (err) {
|
|
|
|
|
setError(err.message);
|
|
|
|
|
return { success: false, error: err.message };
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Logout function
|
|
|
|
|
const logout = async () => {
|
|
|
|
|
try {
|
|
|
|
|
await fetch(`${API_BASE}/auth/logout`, {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
credentials: 'include'
|
|
|
|
|
});
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error('Logout error:', err);
|
|
|
|
|
}
|
|
|
|
|
setUser(null);
|
|
|
|
|
};
|
|
|
|
|
|
2026-04-06 16:18:07 -06:00
|
|
|
// Check if user belongs to one of the specified groups
|
|
|
|
|
const isInGroup = (...groups) => user && groups.includes(user.group);
|
|
|
|
|
|
|
|
|
|
// Check if user can perform write operations (Admin or Standard_User)
|
|
|
|
|
const canWrite = () => isInGroup('Admin', 'Standard_User');
|
|
|
|
|
|
|
|
|
|
// Check if user can delete a resource
|
|
|
|
|
const canDelete = (resource) => {
|
|
|
|
|
if (!user) return false;
|
|
|
|
|
if (isInGroup('Admin')) return true;
|
|
|
|
|
if (!isInGroup('Standard_User')) return false;
|
|
|
|
|
return resource?.created_by === user.id;
|
2026-01-28 14:36:33 -07:00
|
|
|
};
|
|
|
|
|
|
2026-04-06 16:18:07 -06:00
|
|
|
// Check if user can export data
|
|
|
|
|
const canExport = () => isInGroup('Admin', 'Standard_User', 'Leadership');
|
2026-01-28 14:36:33 -07:00
|
|
|
|
|
|
|
|
// Check if user is admin
|
2026-04-06 16:18:07 -06:00
|
|
|
const isAdmin = () => isInGroup('Admin');
|
2026-01-28 14:36:33 -07:00
|
|
|
|
2026-05-05 11:04:53 -06:00
|
|
|
// -----------------------------------------------------------------------
|
|
|
|
|
// Multi-BU tenancy helpers
|
|
|
|
|
// -----------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
// Whether the user has any BU teams assigned
|
|
|
|
|
const hasTeams = () => (user?.teams?.length ?? 0) > 0;
|
|
|
|
|
|
2026-05-05 11:31:15 -06:00
|
|
|
// Whether the user is a member of a specific team
|
2026-05-05 11:04:53 -06:00
|
|
|
const isTeamMember = (team) => {
|
|
|
|
|
if (!user) return false;
|
2026-05-05 11:31:15 -06:00
|
|
|
if (isInGroup('Admin')) {
|
|
|
|
|
// Admin: check against current scope selection
|
|
|
|
|
const scope = adminScope || [];
|
|
|
|
|
return scope.length === 0 || scope.includes(team);
|
|
|
|
|
}
|
2026-05-05 11:04:53 -06:00
|
|
|
return (user.teams || []).includes(team);
|
|
|
|
|
};
|
|
|
|
|
|
2026-05-05 11:31:15 -06:00
|
|
|
// Set the admin scope to a specific set of teams
|
|
|
|
|
const setAdminScopeTeams = (teams) => {
|
|
|
|
|
setAdminScope(teams);
|
|
|
|
|
saveAdminScope(teams);
|
2026-05-05 11:04:53 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Returns the comma-joined teams string for API query params.
|
|
|
|
|
// Empty string means "no filter" (show all).
|
|
|
|
|
const getActiveTeamsParam = () => {
|
|
|
|
|
if (!user) return '';
|
2026-05-05 11:31:15 -06:00
|
|
|
|
|
|
|
|
if (isInGroup('Admin')) {
|
|
|
|
|
const scope = adminScope || [];
|
|
|
|
|
// If all teams selected or empty, no filter
|
|
|
|
|
if (scope.length === 0 || scope.length === KNOWN_TEAMS.length) return '';
|
|
|
|
|
return scope.join(',');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Non-admin: always use their assigned teams
|
2026-05-05 11:04:53 -06:00
|
|
|
const teams = user.teams || [];
|
|
|
|
|
return teams.join(',');
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Returns the list of teams available for UI selectors (compliance team picker, etc.)
|
|
|
|
|
const getAvailableTeams = () => {
|
|
|
|
|
if (!user) return [];
|
2026-05-05 11:31:15 -06:00
|
|
|
|
|
|
|
|
if (isInGroup('Admin')) {
|
|
|
|
|
const scope = adminScope || [];
|
|
|
|
|
// If all selected or empty, show all known teams
|
|
|
|
|
if (scope.length === 0 || scope.length === KNOWN_TEAMS.length) return KNOWN_TEAMS;
|
|
|
|
|
return scope;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-05 11:04:53 -06:00
|
|
|
return user.teams || [];
|
|
|
|
|
};
|
|
|
|
|
|
2026-01-28 14:36:33 -07:00
|
|
|
const value = {
|
|
|
|
|
user,
|
|
|
|
|
loading,
|
|
|
|
|
error,
|
|
|
|
|
login,
|
|
|
|
|
logout,
|
|
|
|
|
checkAuth,
|
2026-04-06 16:18:07 -06:00
|
|
|
isInGroup,
|
2026-01-28 14:36:33 -07:00
|
|
|
canWrite,
|
2026-04-06 16:18:07 -06:00
|
|
|
canDelete,
|
|
|
|
|
canExport,
|
2026-01-28 14:36:33 -07:00
|
|
|
isAdmin,
|
2026-05-05 11:04:53 -06:00
|
|
|
isAuthenticated: !!user,
|
|
|
|
|
// Multi-BU tenancy
|
|
|
|
|
hasTeams,
|
|
|
|
|
isTeamMember,
|
|
|
|
|
adminScope,
|
2026-05-05 11:31:15 -06:00
|
|
|
setAdminScopeTeams,
|
2026-05-05 11:04:53 -06:00
|
|
|
getActiveTeamsParam,
|
|
|
|
|
getAvailableTeams,
|
|
|
|
|
KNOWN_TEAMS,
|
2026-01-28 14:36:33 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<AuthContext.Provider value={value}>
|
|
|
|
|
{children}
|
|
|
|
|
</AuthContext.Provider>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function useAuth() {
|
|
|
|
|
const context = useContext(AuthContext);
|
|
|
|
|
if (!context) {
|
|
|
|
|
throw new Error('useAuth must be used within an AuthProvider');
|
|
|
|
|
}
|
|
|
|
|
return context;
|
|
|
|
|
}
|