This commit is contained in:
Iliyan Angelov
2025-12-01 07:24:31 +02:00
parent 62c1fe5951
commit 49181cf48c
9 changed files with 4519 additions and 5 deletions

View File

@@ -2,6 +2,7 @@ import React, { useState, useEffect } from 'react';
import { useForm } from 'react-hook-form';
import { yupResolver } from '@hookform/resolvers/yup';
import { X, Eye, EyeOff, LogIn, Loader2, Mail, Lock, Shield, ArrowLeft } from 'lucide-react';
import { useNavigate } from 'react-router-dom';
import useAuthStore from '../../../store/useAuthStore';
import { loginSchema, LoginFormData } from '../../../shared/utils/validationSchemas';
import { useCompanySettings } from '../../../shared/contexts/CompanySettingsContext';
@@ -26,8 +27,9 @@ type MFATokenFormData = yup.InferType<typeof mfaTokenSchema>;
const LoginModal: React.FC = () => {
const { closeModal, openModal } = useAuthModal();
const { login, verifyMFA, isLoading, error, clearError, requiresMFA, clearMFA, isAuthenticated } = useAuthStore();
const { login, verifyMFA, isLoading, error, clearError, requiresMFA, clearMFA, isAuthenticated, userInfo } = useAuthStore();
const { settings } = useCompanySettings();
const navigate = useNavigate();
const [showPassword, setShowPassword] = useState(false);
@@ -61,12 +63,26 @@ const LoginModal: React.FC = () => {
},
});
// Close modal on successful authentication
// Close modal and redirect to appropriate dashboard on successful authentication
useEffect(() => {
if (!isLoading && isAuthenticated && !requiresMFA) {
if (!isLoading && isAuthenticated && !requiresMFA && userInfo) {
closeModal();
// Redirect to role-specific dashboard
const role = userInfo.role?.toLowerCase() || (userInfo as any).role_name?.toLowerCase();
if (role === 'admin') {
navigate('/admin/dashboard', { replace: true });
} else if (role === 'staff') {
navigate('/staff/dashboard', { replace: true });
} else if (role === 'accountant') {
navigate('/accountant/dashboard', { replace: true });
} else {
// Customer or default - go to customer dashboard
navigate('/dashboard', { replace: true });
}
}
}, [isLoading, isAuthenticated, requiresMFA, closeModal]);
}, [isLoading, isAuthenticated, requiresMFA, userInfo, closeModal, navigate]);
const {
register,