Add try-catch for MSAL initialization and validate tenant ID

Problem:
- MSAL was throwing runtime errors when Azure credentials were incomplete
- Script error in browser due to invalid MSAL configuration
- PublicClientApplication constructor failing with empty/undefined values

Solution:
- Added tenant ID validation in addition to client ID check
- Wrapped MSAL initialization in try-catch to handle configuration errors gracefully
- Log warning instead of crashing when MSAL cannot be initialized
- Allow application to continue with simple login when Azure is not configured

Changes:
- Check both REACT_APP_AZURE_CLIENT_ID and REACT_APP_AZURE_TENANT_ID
- Use try-catch when creating PublicClientApplication instance
- Set msalInstance to null on initialization failure
- Console warning for debugging when Azure AD is not properly configured

This prevents runtime errors and allows the application to work in test mode
without requiring valid Azure AD configuration.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
SamoilenkoVadym 2026-01-28 11:01:37 +00:00
parent ddaa963bc2
commit c5b17b2bb2

View file

@ -12,12 +12,19 @@ import { authAPI } from '../services/api';
// Check if Azure AD is configured
const isAzureConfigured =
process.env.REACT_APP_AZURE_CLIENT_ID &&
process.env.REACT_APP_AZURE_CLIENT_ID !== 'your-client-id-here';
process.env.REACT_APP_AZURE_CLIENT_ID !== 'your-client-id-here' &&
process.env.REACT_APP_AZURE_TENANT_ID &&
process.env.REACT_APP_AZURE_TENANT_ID !== 'your-tenant-id-here';
// Initialize MSAL instance only if Azure is configured
const msalInstance = isAzureConfigured
? new PublicClientApplication(msalConfig)
: null;
let msalInstance: PublicClientApplication | null = null;
if (isAzureConfigured) {
try {
msalInstance = new PublicClientApplication(msalConfig);
} catch (error) {
console.warn('Failed to initialize MSAL. Azure AD not properly configured:', error);
}
}
interface User {
id: string;