87e9346d62
- NestJS backend with 11 modules: Auth, Employees, Departments, Attendance, Leaves, Payroll, Reimbursements, Announcements, Tax, Reports, Admin - JWT authentication with refresh tokens, role-based access (employee/hr_admin/super_admin) - MongoDB schemas with Mongoose for all entities - PDF payslip generation with pdfkit - OpenTelemetry tracing to SigNoz - Automatic database seeding on first startup - Next.js 14 frontend with App Router, Tailwind CSS - 25 pages covering all employee, HR admin, and super admin workflows - Multi-stage Dockerfile with nginx proxy - test-manifest.json for E2E testing Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
86 lines
2.2 KiB
TypeScript
86 lines
2.2 KiB
TypeScript
'use client';
|
|
import React, { createContext, useContext, useState, useEffect } from 'react';
|
|
import { useRouter } from 'next/navigation';
|
|
import api from './api';
|
|
|
|
interface User {
|
|
_id: string;
|
|
employeeId: string;
|
|
firstName: string;
|
|
lastName: string;
|
|
email: string;
|
|
role: 'employee' | 'hr_admin' | 'super_admin';
|
|
mustChangePassword?: boolean;
|
|
}
|
|
|
|
interface AuthContextType {
|
|
user: User | null;
|
|
loading: boolean;
|
|
login: (employeeId: string, password: string) => Promise<void>;
|
|
logout: () => Promise<void>;
|
|
}
|
|
|
|
const AuthContext = createContext<AuthContextType>({
|
|
user: null,
|
|
loading: true,
|
|
login: async () => {},
|
|
logout: async () => {},
|
|
});
|
|
|
|
export function AuthProvider({ children }: { children: React.ReactNode }) {
|
|
const [user, setUser] = useState<User | null>(null);
|
|
const [loading, setLoading] = useState(true);
|
|
const router = useRouter();
|
|
|
|
useEffect(() => {
|
|
const storedUser = localStorage.getItem('user');
|
|
const token = localStorage.getItem('access_token');
|
|
if (storedUser && token) {
|
|
try {
|
|
setUser(JSON.parse(storedUser));
|
|
} catch {
|
|
localStorage.clear();
|
|
}
|
|
}
|
|
setLoading(false);
|
|
}, []);
|
|
|
|
const login = async (employeeId: string, password: string) => {
|
|
const response = await api.post('/auth/login', { employeeId, password });
|
|
const { accessToken, refreshToken, user: userData } = response.data;
|
|
|
|
localStorage.setItem('access_token', accessToken);
|
|
localStorage.setItem('refresh_token', refreshToken);
|
|
localStorage.setItem('user', JSON.stringify(userData));
|
|
|
|
setUser(userData);
|
|
|
|
if (userData.role === 'super_admin') {
|
|
router.push('/superadmin/dashboard');
|
|
} else if (userData.role === 'hr_admin') {
|
|
router.push('/admin/dashboard');
|
|
} else {
|
|
router.push('/dashboard');
|
|
}
|
|
};
|
|
|
|
const logout = async () => {
|
|
try {
|
|
await api.post('/auth/logout');
|
|
} catch {}
|
|
localStorage.removeItem('access_token');
|
|
localStorage.removeItem('refresh_token');
|
|
localStorage.removeItem('user');
|
|
setUser(null);
|
|
router.push('/login');
|
|
};
|
|
|
|
return (
|
|
<AuthContext.Provider value={{ user, loading, login, logout }}>
|
|
{children}
|
|
</AuthContext.Provider>
|
|
);
|
|
}
|
|
|
|
export const useAuth = () => useContext(AuthContext);
|