-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgetProjects2.js
38 lines (33 loc) · 953 Bytes
/
getProjects2.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import client from '../../src/db';
import jwt from 'jsonwebtoken';
export default async function handler(req, res) {
const {name} = req.query;
const token = req.headers.authorization;
try {
await client.connect();
const db = client.db('ivrStudio');
const collection = db.collection('projects');
let validEmail = '';
if (token) {
const decoded = jwt.verify(token, process.env.JWT_SECRET);
validEmail = decoded.email;
} else {
validEmail = 'guest';
}
const documents = await collection
.find(
{email: validEmail},
{projection: {name: 1, timestamp: 1, description: 1}}
)
.toArray();
if (documents.length === 0) {
res.status(404).json({error: 'No projects found'});
} else {
res.status(200).json(documents);
}
} catch (error) {
res.status(500).json({message: 'Internal server error'});
} finally {
await client.close();
}
}