spa-entra-id
Version:
Single Page Application with Azure Entra ID authentication using MSAL
145 lines (123 loc) ⢠4.78 kB
JavaScript
import { preview } from 'vite'
import { resolve, dirname } from 'path'
import { fileURLToPath } from 'url'
import fs from 'fs'
import { spawn } from 'child_process'
const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)
const PORT = 5001
const HOST = 'localhost'
// Check command line arguments
const args = process.argv.slice(2)
const skipBuild = args.includes('--skip-build')
const trustCert = args.includes('--trust-cert')
function openBrowserWithInstructions() {
console.log('')
console.log('š To access the application:')
console.log('1. Open your browser and go to: https://localhost:5001')
console.log('2. You may see a security warning - this is normal for self-signed certificates')
console.log('3. Click "Advanced" and then "Proceed to localhost (unsafe)"')
console.log('4. Or add the certificate to your browser\'s trusted certificates')
console.log('')
}
async function startServer() {
console.log('š Starting SPA Entra ID Application...')
console.log('š Running in HTTPS mode (required for Azure Entra ID)')
const distPath = resolve(__dirname, 'dist')
// Check if built files exist
if (!fs.existsSync(distPath) && !skipBuild) {
console.log('š¦ Built files not found. Building the application...')
// Import and run build
const { build } = await import('vite')
try {
await build({
root: __dirname,
build: {
outDir: 'dist',
rollupOptions: {
input: {
main: resolve(__dirname, 'index.html'),
auth: resolve(__dirname, 'auth.html')
}
}
}
})
console.log('ā
Build completed successfully!')
} catch (error) {
console.error('ā Build failed:', error.message)
process.exit(1)
}
}
try {
// Use Vite's preview server with HTTPS
const server = await preview({
root: __dirname,
preview: {
port: PORT,
host: HOST,
https: {
// Use basic self-signed certificate
// This is sufficient for Azure Entra ID development
},
open: false
}
})
console.log(`ā
SPA Entra ID Application is running at:`)
console.log(` š https://${HOST}:${PORT}`)
console.log(` š HTTPS enabled for Azure Entra ID authentication`)
openBrowserWithInstructions()
console.log('Press Ctrl+C to stop the server')
console.log('')
console.log('Available options:')
console.log(' --skip-build Skip building if dist folder exists')
// Keep the process alive
process.stdin.resume()
} catch (error) {
console.error('ā Failed to start server:', error.message)
console.log('')
if (error.message.includes('SSL') || error.message.includes('certificate') ||
error.message.includes('EPROTO') || error.message.includes('cipher')) {
console.log('š§ SSL Certificate Issue Detected')
console.log('')
console.log('š” Solutions:')
console.log('1. The application uses self-signed certificates for HTTPS')
console.log('2. This is required for Azure Entra ID authentication')
console.log('3. Your browser will show a security warning - this is expected')
console.log('')
console.log('š To bypass the SSL warning:')
console.log(' ⢠Chrome/Edge: Click "Advanced"ā "Proceed to localhost"')
console.log(' ⢠Firefox: Click "Advanced" ā "Accept the Risk"')
console.log(' ⢠Safari: Click "Show Details" ā "Visit this website"')
console.log('')
console.log('š For production: Use a valid SSL certificate from a CA')
} else if (error.message.includes('EADDRINUSE') || error.message.includes('Port')) {
console.log('š§ Port Issue Detected')
console.log('')
console.log('š” Solutions:')
console.log('1. Stop any other application using port 5001')
console.log('2. Or wait a moment and try again')
console.log('')
console.log('To find and stop the process using port 5001:')
console.log(' lsof -ti:5001 | xargs kill -9')
}
console.log('')
console.log('If issues persist, try running: npm run dev')
console.log('This will use the development server instead.')
process.exit(1)
}
}
// Handle graceful shutdown
process.on('SIGINT', () => {
console.log('\nš Shutting down SPA Entra ID Application...')
process.exit(0)
})
process.on('SIGTERM', () => {
console.log('\nš Shutting down SPA Entra ID Application...')
process.exit(0)
})
// Start the server
startServer().catch((error) => {
console.error('ā Failed to start application:', error)
process.exit(1)
})