Skip to content

Commit 0b971ab

Browse files
committed
Update
1 parent 7ceb8ea commit 0b971ab

1 file changed

Lines changed: 14 additions & 4 deletions

File tree

backend/index.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const app = express();
1111

1212
const pool = new Pool({
1313
connectionString: process.env.DATABASE_URL,
14-
ssl: process.env.NODE_ENV === 'production' ? { rejectUnauthorized: false } : false
14+
ssl: process.env.DATABASE_URL ? { rejectUnauthorized: false } : false // Use SSL if DATABASE_URL is present
1515
});
1616

1717
// Initialize Supabase Client with error handling
@@ -58,7 +58,11 @@ const authenticateToken = async (req, res, next) => {
5858
next();
5959
} catch (error) {
6060
console.error('Error in authentication middleware:', error);
61-
res.status(500).json({ message: 'Internal server error during authentication' });
61+
// Check if the error is specifically from Supabase auth
62+
if (error.message && (error.message.includes('invalid jwt') || error.message.includes('expired jwt') || error.message.includes('invalid claims'))) {
63+
return res.status(403).json({ message: 'Invalid or expired token', error: error.message });
64+
}
65+
res.status(500).json({ message: 'Internal server error during authentication', error: error.message });
6266
}
6367
};
6468

@@ -214,6 +218,7 @@ app.post('/api/login', async (req, res) => {
214218
return res.status(400).json({ message: error.message });
215219
}
216220

221+
console.log('Supabase access token received:', data.session.access_token);
217222
res.status(200).json({
218223
message: 'Login successful',
219224
user: data.user,
@@ -351,6 +356,10 @@ app.delete('/api/account', authenticateToken, async (req, res) => {
351356
// API Routes (Protected)
352357
app.get('/api/projects', authenticateToken, async (req, res) => {
353358
try {
359+
if (!req.user || !req.user.id) {
360+
console.error('Authentication failed: req.user or req.user.id is missing.');
361+
return res.status(401).json({ message: 'Authentication required or failed' });
362+
}
354363
const userId = req.user.id; // Get user ID from authenticated token
355364
console.log('Fetching projects for user:', userId);
356365

@@ -381,8 +390,9 @@ app.get('/api/projects', authenticateToken, async (req, res) => {
381390
console.log(`Found ${projects.length} projects for user ${userId}`);
382391
res.json(projects);
383392
} catch (error) {
384-
console.error('Error fetching projects:', error);
385-
res.status(500).json({ error: 'Failed to fetch projects' });
393+
console.error('Error fetching projects:', error.message);
394+
console.error('Error details:', error);
395+
res.status(500).json({ error: 'Failed to fetch projects', details: error.message });
386396
}
387397
});
388398

0 commit comments

Comments
 (0)