diff --git a/src/index.js b/src/index.js index af08537..7c88291 100644 --- a/src/index.js +++ b/src/index.js @@ -3,8 +3,13 @@ * Modularized Backend */ const express = require('express'); +const path = require('path'); const puppeteer = require('puppeteer'); +// Import config +const { pool } = require('./config/database'); +const { OAuthClient, getOAuthClient, saveTokens } = require('./config/qbo'); + // Import routes const customerRoutes = require('./routes/customers'); const quoteRoutes = require('./routes/quotes'); @@ -49,6 +54,7 @@ async function initBrowser() { browser.on('disconnected', () => { console.log('[BROWSER] Browser disconnected, restarting...'); browser = null; + setBrowser(null); initBrowser(); }); } @@ -57,9 +63,42 @@ async function initBrowser() { // Middleware app.use(express.json()); -app.use(express.static('public')); +app.use(express.static(path.join(__dirname, '..', 'public'))); -// Mount routes +// ===================================================== +// QBO OAuth Routes — mounted at root level (not under /api/qbo) +// These must match the Intuit callback URL configuration +// ===================================================== +app.get('/auth/qbo', (req, res) => { + const client = getOAuthClient(); + const authUri = client.authorizeUri({ + scope: [OAuthClient.scopes.Accounting], + state: 'intuit-qbo-auth' + }); + console.log('🔗 Redirecting to QBO Authorization:', authUri); + res.redirect(authUri); +}); + +app.get('/auth/qbo/callback', async (req, res) => { + const client = getOAuthClient(); + try { + const authResponse = await client.createToken(req.url); + console.log('✅ QBO Authorization erfolgreich!'); + saveTokens(); + res.redirect('/#settings'); + } catch (e) { + console.error('❌ QBO Authorization fehlgeschlagen:', e); + res.status(500).send(` +

QBO Authorization Failed

+

${e.message || e}

+ Zurück zur App + `); + } +}); + +// ===================================================== +// API Routes +// ===================================================== app.use('/api/customers', customerRoutes); app.use('/api/quotes', quoteRoutes); app.use('/api/invoices', invoiceRoutes); @@ -81,11 +120,10 @@ process.on('SIGTERM', async () => { if (browser) { await browser.close(); } - const { pool } = require('./config/database'); await pool.end(); process.exit(0); }); startServer(); -module.exports = app; +module.exports = app; \ No newline at end of file diff --git a/src/routes/qbo.js b/src/routes/qbo.js index b72a537..be9b417 100644 --- a/src/routes/qbo.js +++ b/src/routes/qbo.js @@ -1,6 +1,7 @@ /** * QBO Routes - * Handles QBO OAuth, sync, and data operations + * Handles QBO sync and data operations + * NOTE: OAuth auth/callback routes are in index.js (root-level paths) */ const express = require('express'); const router = express.Router(); @@ -22,35 +23,6 @@ router.get('/status', (req, res) => { } }); -// GET auth URL - redirects to Intuit -router.get('/auth', (req, res) => { - const client = getOAuthClient(); - const authUri = client.authorizeUri({ - scope: [require('../config/qbo').OAuthClient.scopes.Accounting], - state: 'intuit-qbo-auth' - }); - console.log('🔗 Redirecting to QBO Authorization:', authUri); - res.redirect(authUri); -}); - -// OAuth callback -router.get('/auth/callback', async (req, res) => { - const client = getOAuthClient(); - try { - const authResponse = await client.createToken(req.url); - console.log('✅ QBO Authorization erfolgreich!'); - saveTokens(); - res.redirect('/#settings'); - } catch (e) { - console.error('❌ QBO Authorization fehlgeschlagen:', e); - res.status(500).send(` -

QBO Authorization Failed

-

${e.message || e}

- Zurück zur App - `); - } -}); - // GET bank accounts from QBO router.get('/accounts', async (req, res) => { try { @@ -597,4 +569,4 @@ router.post('/sync-payments', async (req, res) => { } }); -module.exports = router; +module.exports = router; \ No newline at end of file