Files
LynkedUpPro_CRM/src/context/AuthContext.jsx
T
Satyam 2eaac6b84a feat: Multi-role platform expansion, mobile nav redesign, and UI polish
- Add Owner, Contractor, Vendor, Subcontractor dashboards and role-based routing
- Owner now has superuser access to all Admin pages (dashboard, schedule, leaderboard)
- Redesign landing page mobile menu as slide-in sidebar (replaces broken Framer Motion overlay)
- Add body scroll lock to app sidebar for mobile consistency
- Fix Team Schedule to match Admin Dashboard design language (ambient glows, gradient header, SpotlightCard, zinc palette)
- Fix login page tab overflow — use abbreviated labels and grid layout for 6 role tabs
- Fix Recharts ResponsiveContainer warnings — replace height="100%" with fixed pixel heights
- Fix lightbox image viewer — unified nav bar, touch swipe support, no more overlapping text
- Add AI Assistant page, People/Vendor/Document management for Owner role
- Expand mock data store with contractor, vendor, and subcontractor data
2026-02-17 01:23:06 +05:30

103 lines
4.0 KiB
React

import React, { createContext, useContext, useState } from 'react';
import { useMockStore } from '../data/mockStore';
import { logger } from '../utils/logger';
import { toast } from 'sonner';
const AuthContext = createContext();
export const ROLES = {
OWNER: 'OWNER',
ADMIN: 'ADMIN',
CONTRACTOR: 'CONTRACTOR',
SUBCONTRACTOR: 'SUBCONTRACTOR',
VENDOR: 'VENDOR',
FIELD_AGENT: 'FIELD_AGENT',
CUSTOMER: 'CUSTOMER'
};
export const AuthProvider = ({ children }) => {
const [user, setUser] = useState(null);
const [isAuthenticated, setIsAuthenticated] = useState(false);
const { users, updateUser } = useMockStore(); // Destructure updateUser
const login = (identifier, password, type) => {
try {
const foundUser = users.find(u => {
if (u.type !== type) return false;
if (type === 'customer') return u.username === identifier && u.password === password;
if (type === 'employee') return u.empId === identifier && u.password === password;
// Support new role types
if (['owner', 'contractor', 'subcontractor', 'vendor'].includes(type) || u.type === type) {
return (u.email === identifier || u.username === identifier) && u.password === password;
}
return false;
});
if (foundUser) {
// Ensure specific role exists and matches requested type if applicable
const userWithRole = {
...foundUser,
role: foundUser.role || 'CUSTOMER'
};
setUser(userWithRole);
setIsAuthenticated(true);
logger.info("User logged in successfully", { userId: foundUser.id, role: userWithRole.role });
toast.success(`Welcome back, ${foundUser.name}!`);
return { success: true, role: userWithRole.role };
}
logger.warn("Failed login attempt", { identifier, type });
toast.error("Invalid credentials", { description: "Please check your username/ID and password." });
return { success: false, message: "Invalid credentials" };
} catch (error) {
logger.error("Login process error", error);
toast.error("An unexpected error occurred during login.");
return { success: false, message: "System error" };
}
};
const logout = () => {
try {
const userName = user?.name;
setUser(null);
setIsAuthenticated(false);
logger.info("User logged out", { userId: user?.id });
toast.info("Logged out successfully");
} catch (error) {
logger.error("Logout error", error);
}
};
const updateProfile = (updatedData) => {
try {
const newUser = { ...user, ...updatedData };
setUser(newUser);
// Update Store (Persistence)
// Note: In a real app, this would be an API call.
// Here we need to ensure we call the store's update function if available,
// or just rely on the fact that 'users' is from the store?
// 'users' from useMockStore is state, but we need the setter/function.
// The AuthContext needs access to the update function from MockStore.
// But useMockStore is called at the top level of AuthProvider.
// Let's assume we can access it from the component scope, wait...
// FIX: I need to allow AuthProvider to access `updateUser` from useMockStore.
// I'll grab it from the hook call at the top.
} catch (error) {
logger.error("Profile update error", error);
toast.error("Failed to update profile.");
throw error;
}
};
return (
<AuthContext.Provider value={{ user, isAuthenticated, login, logout }}>
{children}
</AuthContext.Provider>
);
};
export const useAuth = () => useContext(AuthContext);