import React, { useState } from 'react';
import { useMsal } from '@azure/msal-react';
import { BarclaysLogo } from './icons/BarclaysLogo';
import { XIcon } from './icons/XIcon';
import { MicrosoftLogo } from './icons/MicrosoftLogo';
import { loginRequest } from '../services/authConfig';
const API_URL = import.meta.env.VITE_BACKEND_URL || 'http://localhost:8000';
const SupportModal: React.FC<{
isOpen: boolean;
onClose: () => void;
}> = ({ isOpen, onClose }) => {
const [query, setQuery] = useState('');
const [isSubmitting, setIsSubmitting] = useState(false);
const [submitStatus, setSubmitStatus] = useState<{ type: 'success' | 'error'; message: string } | null>(null);
if (!isOpen) return null;
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
if (!query.trim()) return;
setIsSubmitting(true);
setSubmitStatus(null);
try {
const response = await fetch(`${API_URL}/api/support/email`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
message: query,
subject: 'Support Request from Mod Comms Login',
}),
});
if (!response.ok) {
throw new Error('Failed to send');
}
setSubmitStatus({ type: 'success', message: 'Thank you for your query. A member of the support team will be in touch with you shortly.' });
setQuery('');
setTimeout(() => {
onClose();
setSubmitStatus(null);
}, 3000);
} catch {
setSubmitStatus({ type: 'error', message: 'Failed to send your message. Please try again later.' });
} finally {
setIsSubmitting(false);
}
};
return (
e.stopPropagation()}
>
Contact Support
);
};
export const Login: React.FC = () => {
const { instance } = useMsal();
const [isSupportModalOpen, setIsSupportModalOpen] = useState(false);
const [isLoggingIn, setIsLoggingIn] = useState(false);
const [loginError, setLoginError] = useState(null);
const handleMicrosoftLogin = async () => {
console.log('[MSAL Login] Starting Microsoft login popup...');
console.log('[MSAL Login] Login request scopes:', loginRequest.scopes);
setIsLoggingIn(true);
setLoginError(null);
try {
const response = await instance.loginPopup(loginRequest);
// Success - MSAL Provider will detect the login and re-render App
console.log('[MSAL Login] Login successful!');
console.log('[MSAL Login] Account:', response.account?.username);
console.log('[MSAL Login] Token type:', response.tokenType);
console.log('[MSAL Login] Expires on:', response.expiresOn);
} catch (error: unknown) {
console.error('[MSAL Login] Login failed:', error);
if (error instanceof Error) {
// Handle user cancellation differently from errors
if (error.message.includes('user_cancelled')) {
console.log('[MSAL Login] User cancelled login');
setLoginError(null); // Don't show error for cancellation
} else {
console.error('[MSAL Login] Error details:', error.message);
setLoginError('Login failed. Please try again or contact support.');
}
}
} finally {
setIsLoggingIn(false);
}
};
return (
<>
setIsSupportModalOpen(false)}
/>
{/* Modern Glassy Background */}
{/* Top Left Blob */}
{/* Bottom Right Blob */}
{/* Noise Texture */}
{/* Login Card */}
Mod Comms
Proof Review & Compliance Platform
Enterprise Sign-In
This application is connected to Azure Active Directory. Please sign in using your corporate Microsoft account.
{loginError && (
{loginError}
)}
>
);
};