- Added `SetupService` to handle the generation and validation of setup tokens. - Integrated setup token generation during application startup if no admin users exist. - Created API endpoints for checking setup status and completing the initial setup. - Updated `AuthService` to include functionality for creating the initial admin user. - Enhanced error handling for setup and authentication processes. - Added frontend components for login and protected routes. - Implemented Zustand store for managing authentication state. - Updated Vite configuration to check setup status and serve the setup page if required. - Documented the initial setup process in `setup.md`.
84 lines
2.4 KiB
TypeScript
84 lines
2.4 KiB
TypeScript
import { defineConfig } from 'vite'
|
|
import react from '@vitejs/plugin-react'
|
|
import path from 'path'
|
|
import http from 'http'
|
|
|
|
// Plugin to check setup status and serve setup page from backend if needed
|
|
const setupCheckPlugin = () => ({
|
|
name: 'setup-check',
|
|
configureServer(server) {
|
|
server.middlewares.use(async (req, res, next) => {
|
|
// Only check root paths
|
|
if (req.url !== '/' && req.url !== '/index.html') {
|
|
return next()
|
|
}
|
|
|
|
try {
|
|
// Check setup status from backend
|
|
const setupStatus = await new Promise<{ setup_required: boolean }>((resolve, reject) => {
|
|
const request = http.get('http://localhost:8080/api/v1/auth/setup-status', (response) => {
|
|
let data = ''
|
|
response.on('data', chunk => data += chunk)
|
|
response.on('end', () => {
|
|
try {
|
|
resolve(JSON.parse(data))
|
|
} catch (e) {
|
|
reject(e)
|
|
}
|
|
})
|
|
})
|
|
request.on('error', reject)
|
|
request.setTimeout(3000, () => reject(new Error('Timeout')))
|
|
})
|
|
|
|
// If setup required, proxy the setup page from backend
|
|
if (setupStatus.setup_required) {
|
|
// Fetch setup page from backend
|
|
const setupPage = await new Promise<string>((resolve, reject) => {
|
|
const request = http.get('http://localhost:8080/', (response) => {
|
|
let data = ''
|
|
response.on('data', chunk => data += chunk)
|
|
response.on('end', () => resolve(data))
|
|
})
|
|
request.on('error', reject)
|
|
request.setTimeout(5000, () => reject(new Error('Timeout')))
|
|
})
|
|
|
|
// Serve the setup page
|
|
res.setHeader('Content-Type', 'text/html')
|
|
res.end(setupPage)
|
|
return
|
|
}
|
|
} catch (err) {
|
|
// If backend is not running or error occurs, continue to app
|
|
console.warn('[setup-check] Could not check setup status, assuming setup complete')
|
|
}
|
|
|
|
next()
|
|
})
|
|
},
|
|
})
|
|
|
|
// https://vitejs.dev/config/
|
|
export default defineConfig({
|
|
plugins: [react(), setupCheckPlugin()],
|
|
resolve: {
|
|
alias: {
|
|
'@': path.resolve(__dirname, './src'),
|
|
},
|
|
},
|
|
build: {
|
|
outDir: 'dist',
|
|
emptyOutDir: true,
|
|
},
|
|
server: {
|
|
port: 3000,
|
|
proxy: {
|
|
'/api': {
|
|
target: 'http://localhost:8080',
|
|
changeOrigin: true,
|
|
},
|
|
},
|
|
},
|
|
})
|