-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgetProject2.js
40 lines (33 loc) · 971 Bytes
/
getProject2.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
39
40
import client from '../../src/db';
import jwt from 'jsonwebtoken';
export default async function handler(req, res) {
if (req.method !== 'GET') {
res.status(405).json({message: 'Method not allowed'});
return;
}
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 query = {email: validEmail, name};
const result = await collection.findOne(query);
if (!result) {
res.status(404).json({message: 'Document not found'});
} else {
res.status(200).json(result);
}
} catch (err) {
res.status(500).json({message: 'Internal server error'});
} finally {
await client.close();
}
}