blob: 508e6cd92df2fe4570c3a3c886e43550958e577a (
plain) (
blame)
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
41
42
43
44
45
|
import { rateLimitStrict, redis } from "@/lib/redis";
// import { getServerSession } from "next-auth";
// import { authOptions } from "pages/api/auth/[...nextauth]";
export default async function handler(req, res) {
// const session = await getServerSession(req, res, authOptions);
// const admin = session?.user?.name === process.env.ADMIN_USERNAME;
// create random id each time the endpoint is called
const id = Math.random().toString(36).substr(2, 9);
try {
if (redis) {
try {
const ipAddress = req.socket.remoteAddress;
await rateLimitStrict.consume(ipAddress);
} catch (error) {
return res.status(429).json({
error: `Too Many Requests, retry after ${error.msBeforeNext / 1000}`,
});
}
if (req.method === "POST") {
const { data } = req.body;
data.id = id;
await redis.set(`report:${id}`, JSON.stringify(data));
return res
.status(200)
.json({ message: `Report has successfully sent, with Id of ${id}` });
} else if (req.method === "DELETE") {
const { reportId } = req.body;
await redis.del(`report:${reportId}`);
return res.status(200).json({ message: `Report has been deleted` });
} else {
return res.status(405).json({ message: "Method not allowed" });
}
}
return res.status(200).json({ message: "redis is not defined" });
} catch (err) {
console.error(err);
res.status(500).json({ error: err.message });
}
}
|