Skip to content

Commit

Permalink
fix: post req
Browse files Browse the repository at this point in the history
  • Loading branch information
0xtiti committed May 8, 2024
1 parent af4c591 commit 3e2d271
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 22 deletions.
26 changes: 11 additions & 15 deletions src/pages/api/logs.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
import type { NextApiRequest, NextApiResponse } from 'next';

export default async function handler(req: NextApiRequest, res: NextApiResponse) {
const referer = req.headers.referer;
if (req.method === 'POST') {
try {
const { id, merkle_root, nullifier_hash, proof, error } = req.body;
const logData = { id, merkle_root, nullifier_hash, proof, error };

const allowedReferers = ['https://www.moregoats.com/', 'https://dev.moregoats.com/'];
console.log(logData);

if (!referer || !allowedReferers.includes(referer)) {
res.status(403).json({ error: 'Unauthorized access' });
return;
}

if (req.method === 'GET') {
const { id, merkle_root, nullifier_hash, proof, error } = req.query;
const logData = { id, merkle_root, nullifier_hash, proof, error };

console.log(logData);

res.status(200).json({ message: 'Log successful', log: logData });
res.status(200).json({ message: 'Log successful', log: logData });
} catch (error) {
console.error('Error processing the request:', error);
res.status(500).json({ error: 'Error processing the request' });
}
} else {
res.setHeader('Allow', ['GET']);
res.setHeader('Allow', ['POST']);
res.status(405).end(`Method ${req.method} Not Allowed`);
}
}
13 changes: 6 additions & 7 deletions src/utils/logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ interface LogEntry {

export async function sendLog(data: LogEntry) {
try {
const response = await fetch(
`/api/logs?id=${data.id}&proof=${data.proof}&merkle_root=${data.merkle_root}&nullifier_hash=${
data.nullifier_hash
}&error=${data.error || null}`,
{
method: 'GET',
const response = await fetch('/api/logs', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
);
body: JSON.stringify(data),
});

if (!response.ok) {
throw new Error('Failed to send log data');
Expand Down

0 comments on commit 3e2d271

Please sign in to comment.