feat: wire real auth — login returns user profile with role from platform, remove mock users and role selector tabs

This commit is contained in:
2026-03-18 10:42:54 +05:30
parent 832fa31597
commit 66ad398b81
4 changed files with 82 additions and 72 deletions

View File

@@ -1,55 +1,46 @@
import type { ReactNode } from 'react';
import { createContext, useContext, useState } from 'react';
import { createContext, useCallback, useContext, useState } from 'react';
export type Role = 'executive' | 'admin' | 'cc-agent';
type User = {
id?: string;
name: string;
initials: string;
role: Role;
email: string;
avatarUrl?: string;
platformRoles?: string[];
};
type AuthContextType = {
user: User;
setRole: (role: Role) => void;
isAdmin: boolean;
isCCAgent: boolean;
isAuthenticated: boolean;
loginWithUser: (userData: User) => void;
login: () => void;
logout: () => void;
setRole: (role: Role) => void;
};
const USERS: Record<Role, User> = {
admin: {
name: 'Admin User',
initials: 'AU',
role: 'admin',
email: 'admin@globalhospital.com',
},
executive: {
name: 'Sanjay M.',
initials: 'SM',
role: 'executive',
email: 'sanjay@globalhospital.com',
},
'cc-agent': {
name: 'Rekha S.',
initials: 'RS',
role: 'cc-agent' as const,
email: 'rekha@globalhospital.com',
},
const DEFAULT_USER: User = {
name: '',
initials: '',
role: 'executive',
email: '',
};
const getInitials = (firstName: string, lastName: string): string =>
`${firstName[0] ?? ''}${lastName[0] ?? ''}`.toUpperCase();
const AuthContext = createContext<AuthContextType | undefined>(undefined);
export const useAuth = (): AuthContextType => {
const context = useContext(AuthContext);
if (context === undefined) {
throw new Error('useAuth must be used within an AuthProvider');
}
return context;
};
@@ -58,19 +49,39 @@ interface AuthProviderProps {
}
export const AuthProvider = ({ children }: AuthProviderProps) => {
const [role, setRole] = useState<Role>('executive');
const [user, setUser] = useState<User>(DEFAULT_USER);
const [isAuthenticated, setIsAuthenticated] = useState(false);
const user = USERS[role];
const isAdmin = role === 'admin';
const isCCAgent = role === 'cc-agent';
const isAdmin = user.role === 'admin';
const isCCAgent = user.role === 'cc-agent';
const login = () => setIsAuthenticated(true);
const logout = () => setIsAuthenticated(false);
// Real login — receives user profile from sidecar auth response
const loginWithUser = useCallback((userData: User) => {
setUser(userData);
setIsAuthenticated(true);
}, []);
// Simple login (for backward compat)
const login = useCallback(() => {
setIsAuthenticated(true);
}, []);
const logout = useCallback(() => {
setUser(DEFAULT_USER);
setIsAuthenticated(false);
localStorage.removeItem('helix_access_token');
localStorage.removeItem('helix_refresh_token');
}, []);
const setRole = useCallback((role: Role) => {
setUser(prev => ({ ...prev, role }));
}, []);
return (
<AuthContext.Provider value={{ user, setRole, isAdmin, isCCAgent, isAuthenticated, login, logout }}>
<AuthContext.Provider value={{ user, isAdmin, isCCAgent, isAuthenticated, loginWithUser, login, logout, setRole }}>
{children}
</AuthContext.Provider>
);
};
export { getInitials };