import { useState, useEffect } from 'react'; import { PublicClientApplication } from '@azure/msal-browser'; import { msalConfig, loginRequest } from '../msalConfig'; import './Login.css'; function Login({ onLoginSuccess, onSwitchToRegister }) { const [username, setUsername] = useState(''); const [password, setPassword] = useState(''); const [error, setError] = useState(''); const [loading, setLoading] = useState(false); const [msalInstance, setMsalInstance] = useState(null); const [autoLoginAttempted, setAutoLoginAttempted] = useState (false); useEffect(() => { const initializeMasal = async () => { const instance = new PublicClientApplication(msalConfig); await instance.initialize(); setMsalInstance(instance); }; initializeMasal(); }, []); useEffect(() => { const manualLogout = localStorage.getItem('manualLogout'); if (msalInstance && !autoLoginAttempted && manualLogout !== 'true') { setAutoLoginAttempted(true); handleMicrosoftLogin(); } }, [msalInstance]) const handleSubmit = async (e) => { e.preventDefault(); setError(''); setLoading(true); try { const response = await fetch('http://localhost:9000/api/auth/login', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ username: username, password: password }) }); const data = await response.json(); if (response.ok) { localStorage.removeItem('manualLogout'); onLoginSuccess(data.user, data.token); } else { setError(data.error || 'Login failed'); } } catch (err) { console.error('Login error:', err); setError('Failed to connect to server'); } finally { setLoading(false); } }; const handleMicrosoftLogin = async () => { if (!msalInstance) { setError('Microsoft login is initializing, please try again'); return; } setLoading(true); setError(''); try { // Trigger Microsoft login popup - this gets us the access token directly const loginResponse = await msalInstance.loginPopup(loginRequest); // Send the access token to your backend const response = await fetch('http://localhost:9000/api/auth/microsoft', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ accessToken: loginResponse.accessToken, idToken: loginResponse.idToken }) }); const data = await response.json(); if (response.ok) { localStorage.removeItem('manualLogout'); onLoginSuccess(data.user, data.token); } else { setError(data.error || 'Microsoft login failed'); } } catch (err) { console.error('Microsoft login error:', err); setError('Failed to login with Microsoft'); } finally { setLoading(false); } }; return (

Login to Knowledge Base

{error &&
{error}
}
setUsername(e.target.value)} required disabled={loading} />
setPassword(e.target.value)} required disabled={loading} />
OR
); } export default Login;