From 40fe880fd62f9d3ff767f46e635283a6c253f60d Mon Sep 17 00:00:00 2001 From: titix Date: Wed, 8 May 2024 13:19:48 -0300 Subject: [PATCH 01/12] fix: 255 limit char event --- src/components/Voting.tsx | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/components/Voting.tsx b/src/components/Voting.tsx index 0dfb80e..86d98fa 100644 --- a/src/components/Voting.tsx +++ b/src/components/Voting.tsx @@ -60,11 +60,14 @@ export const Voting = () => { try { // Get the proof data const { merkle_root, nullifier_hash, proof } = result; - if (address && merkle_root && nullifier_hash && proof) { - const proofStr = `merkleRoot: ${merkle_root} nullifierHash: ${nullifier_hash} proof: ${proof}`; - track('Voting proof', { + if (address && merkle_root && nullifier_hash) { + track('Voting merkle root', { address, - proofStr, + merkle_root, + }); + track('Voting nullifier hash', { + address, + nullifier_hash, }); } console.log('Voting proof:', result); @@ -75,7 +78,7 @@ export const Voting = () => { nullifier_hash as Hex, ); const [decodedProof] = decodeAbiParameters(parseAbiParameters('uint256[8] proof'), proof as Hex); - + console.log(decodedProof); const proofData = encodePacked( ['uint256', 'uint256', 'uint256[8]'], [decodedMerfleRoot, decodedNullifierHash, decodedProof], @@ -116,9 +119,14 @@ export const Voting = () => { setModalOpen(ModalType.ERROR); } } catch (error) { + const errorString = String(error); + const truncatedError = errorString.length > 255 ? errorString.substring(0, 255) : errorString; console.error('Cast failed:', error); - if (error instanceof Error && address) { - track('Error', { message: error.message || 'Unknown error', address }); + if (truncatedError && address) { + track('Error', { + address, + truncatedError, + }); } setModalOpen(ModalType.ERROR); } From d9490bfd70206e83e82e5e97fd214cde04248d0f Mon Sep 17 00:00:00 2001 From: titix Date: Wed, 8 May 2024 14:30:45 -0300 Subject: [PATCH 02/12] fix: error logs api --- src/components/Voting.tsx | 28 +++++++++++----------------- src/pages/api/logs.ts | 12 ++++++++++++ src/utils/index.ts | 1 + src/utils/logs.ts | 20 ++++++++++++++++++++ 4 files changed, 44 insertions(+), 17 deletions(-) create mode 100644 src/pages/api/logs.ts create mode 100644 src/utils/logs.ts diff --git a/src/components/Voting.tsx b/src/components/Voting.tsx index 86d98fa..5405f7e 100644 --- a/src/components/Voting.tsx +++ b/src/components/Voting.tsx @@ -11,6 +11,7 @@ import { track } from '@vercel/analytics'; import { useContract, useCustomTheme, useModal, useVote } from '~/hooks'; import { ModalType } from '~/types'; import { getConfig } from '~/config'; +import { addLog } from '~/utils'; const { APP_ID, PROPOSAL_ID } = getConfig(); @@ -60,17 +61,13 @@ export const Voting = () => { try { // Get the proof data const { merkle_root, nullifier_hash, proof } = result; - if (address && merkle_root && nullifier_hash) { - track('Voting merkle root', { - address, - merkle_root, - }); - track('Voting nullifier hash', { - address, - nullifier_hash, + console.log('Voting proof:', result); + if (address) { + addLog({ + id: address, + proof: result, }); } - console.log('Voting proof:', result); const [decodedMerfleRoot] = decodeAbiParameters(parseAbiParameters('uint256 merkle_root'), merkle_root as Hex); const [decodedNullifierHash] = decodeAbiParameters( @@ -78,7 +75,6 @@ export const Voting = () => { nullifier_hash as Hex, ); const [decodedProof] = decodeAbiParameters(parseAbiParameters('uint256[8] proof'), proof as Hex); - console.log(decodedProof); const proofData = encodePacked( ['uint256', 'uint256', 'uint256[8]'], [decodedMerfleRoot, decodedNullifierHash, decodedProof], @@ -99,7 +95,6 @@ export const Voting = () => { const receipt = await publicClient.waitForTransactionReceipt({ hash: hash as Hex, }); - console.log('Tx receipt:', receipt); if (receipt) { setTxDone(true); @@ -119,13 +114,12 @@ export const Voting = () => { setModalOpen(ModalType.ERROR); } } catch (error) { - const errorString = String(error); - const truncatedError = errorString.length > 255 ? errorString.substring(0, 255) : errorString; console.error('Cast failed:', error); - if (truncatedError && address) { - track('Error', { - address, - truncatedError, + if (address) { + addLog({ + id: address, + proof: result, + error: String(error), }); } setModalOpen(ModalType.ERROR); diff --git a/src/pages/api/logs.ts b/src/pages/api/logs.ts new file mode 100644 index 0000000..6b8c5de --- /dev/null +++ b/src/pages/api/logs.ts @@ -0,0 +1,12 @@ +import type { NextApiRequest, NextApiResponse } from 'next'; +import { getLogs } from '~/utils'; + +export default function handler(req: NextApiRequest, res: NextApiResponse) { + if (req.method === 'GET') { + const logs = getLogs(); + res.status(200).json(logs); + } else { + res.setHeader('Allow', ['GET']); + res.status(405).end(`Method ${req.method} Not Allowed`); + } +} diff --git a/src/utils/index.ts b/src/utils/index.ts index 7697229..cca275c 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -4,3 +4,4 @@ export * from './config'; export * from './misc'; export * from './Variables'; export * from './parsedAbi'; +export * from './logs'; diff --git a/src/utils/logs.ts b/src/utils/logs.ts new file mode 100644 index 0000000..602d75a --- /dev/null +++ b/src/utils/logs.ts @@ -0,0 +1,20 @@ +interface LogEntry { + id: `0x${string}`; + proof: { + merkle_root: string; + nullifier_hash: string; + proof: string; + }; + error?: string; +} + +const logs: LogEntry[] = []; + +export const addLog = (entry: LogEntry) => { + logs.push({ ...entry }); + console.log('Log added:', entry); +}; + +export const getLogs = (): LogEntry[] => { + return logs; +}; From cda7d300f9d1bee967f7acf511a1be43ce27643c Mon Sep 17 00:00:00 2001 From: titix Date: Wed, 8 May 2024 15:18:26 -0300 Subject: [PATCH 03/12] fix: logs --- src/pages/api/logs.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/pages/api/logs.ts b/src/pages/api/logs.ts index 6b8c5de..8dae436 100644 --- a/src/pages/api/logs.ts +++ b/src/pages/api/logs.ts @@ -4,6 +4,7 @@ import { getLogs } from '~/utils'; export default function handler(req: NextApiRequest, res: NextApiResponse) { if (req.method === 'GET') { const logs = getLogs(); + console.log(logs); res.status(200).json(logs); } else { res.setHeader('Allow', ['GET']); From 1626def3ebde42676a6c228666c0b6a8dd82f3fa Mon Sep 17 00:00:00 2001 From: titix Date: Wed, 8 May 2024 15:37:42 -0300 Subject: [PATCH 04/12] fix: console.log on backend --- src/components/Voting.tsx | 6 +++--- src/pages/api/logs.ts | 9 +++++---- src/utils/logs.ts | 22 ++++++++++++++-------- 3 files changed, 22 insertions(+), 15 deletions(-) diff --git a/src/components/Voting.tsx b/src/components/Voting.tsx index 5405f7e..084960a 100644 --- a/src/components/Voting.tsx +++ b/src/components/Voting.tsx @@ -11,7 +11,7 @@ import { track } from '@vercel/analytics'; import { useContract, useCustomTheme, useModal, useVote } from '~/hooks'; import { ModalType } from '~/types'; import { getConfig } from '~/config'; -import { addLog } from '~/utils'; +import { sendLog } from '~/utils'; const { APP_ID, PROPOSAL_ID } = getConfig(); @@ -63,7 +63,7 @@ export const Voting = () => { const { merkle_root, nullifier_hash, proof } = result; console.log('Voting proof:', result); if (address) { - addLog({ + sendLog({ id: address, proof: result, }); @@ -116,7 +116,7 @@ export const Voting = () => { } catch (error) { console.error('Cast failed:', error); if (address) { - addLog({ + sendLog({ id: address, proof: result, error: String(error), diff --git a/src/pages/api/logs.ts b/src/pages/api/logs.ts index 8dae436..4ef3022 100644 --- a/src/pages/api/logs.ts +++ b/src/pages/api/logs.ts @@ -1,11 +1,12 @@ import type { NextApiRequest, NextApiResponse } from 'next'; -import { getLogs } from '~/utils'; export default function handler(req: NextApiRequest, res: NextApiResponse) { if (req.method === 'GET') { - const logs = getLogs(); - console.log(logs); - res.status(200).json(logs); + const { id, proof, error } = req.query; + + console.log({ id, proof, error }); + + res.status(200).json({ message: 'Log received successfully' }); } else { res.setHeader('Allow', ['GET']); res.status(405).end(`Method ${req.method} Not Allowed`); diff --git a/src/utils/logs.ts b/src/utils/logs.ts index 602d75a..b0a104d 100644 --- a/src/utils/logs.ts +++ b/src/utils/logs.ts @@ -8,13 +8,19 @@ interface LogEntry { error?: string; } -const logs: LogEntry[] = []; +export async function sendLog(data: LogEntry) { + try { + const response = await fetch(`/api/log?id=${data.id}&proof=${data.proof}&error=${data.error || null}`, { + method: 'GET', + }); -export const addLog = (entry: LogEntry) => { - logs.push({ ...entry }); - console.log('Log added:', entry); -}; + if (!response.ok) { + throw new Error('Failed to send log data'); + } -export const getLogs = (): LogEntry[] => { - return logs; -}; + const responseData = await response.json(); + console.log('Server log response:', responseData); + } catch (error) { + console.error('Failed to send log:', error); + } +} From d10a6771efa486265636759d4a0a4f6964d02f5a Mon Sep 17 00:00:00 2001 From: titix Date: Wed, 8 May 2024 15:46:31 -0300 Subject: [PATCH 05/12] fix: response --- src/pages/api/logs.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/pages/api/logs.ts b/src/pages/api/logs.ts index 4ef3022..92c9084 100644 --- a/src/pages/api/logs.ts +++ b/src/pages/api/logs.ts @@ -3,10 +3,11 @@ import type { NextApiRequest, NextApiResponse } from 'next'; export default function handler(req: NextApiRequest, res: NextApiResponse) { if (req.method === 'GET') { const { id, proof, error } = req.query; + const logData = { id, proof, error }; - console.log({ id, proof, error }); + console.log(logData); - res.status(200).json({ message: 'Log received successfully' }); + res.status(200).json({ message: 'Log successful', log: logData }); } else { res.setHeader('Allow', ['GET']); res.status(405).end(`Method ${req.method} Not Allowed`); From 77618d21aa1a98ad90ec6b9d8326683a70f094ef Mon Sep 17 00:00:00 2001 From: titix Date: Wed, 8 May 2024 15:57:22 -0300 Subject: [PATCH 06/12] fix: destructuring --- src/components/Voting.tsx | 12 ++++++++---- src/utils/logs.ts | 19 +++++++++++-------- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/src/components/Voting.tsx b/src/components/Voting.tsx index 084960a..bd18010 100644 --- a/src/components/Voting.tsx +++ b/src/components/Voting.tsx @@ -58,14 +58,16 @@ export const Voting = () => { const onSuccess = useCallback( async (result: ISuccessResult) => { + // Get the proof data + const { merkle_root, nullifier_hash, proof } = result; try { - // Get the proof data - const { merkle_root, nullifier_hash, proof } = result; console.log('Voting proof:', result); if (address) { sendLog({ id: address, - proof: result, + proof: proof, + merkle_root: merkle_root, + nullifier_hash: nullifier_hash, }); } @@ -118,7 +120,9 @@ export const Voting = () => { if (address) { sendLog({ id: address, - proof: result, + proof: proof, + merkle_root: merkle_root, + nullifier_hash: nullifier_hash, error: String(error), }); } diff --git a/src/utils/logs.ts b/src/utils/logs.ts index b0a104d..7cc9145 100644 --- a/src/utils/logs.ts +++ b/src/utils/logs.ts @@ -1,18 +1,21 @@ interface LogEntry { id: `0x${string}`; - proof: { - merkle_root: string; - nullifier_hash: string; - proof: string; - }; + merkle_root: string; + nullifier_hash: string; + proof: string; error?: string; } export async function sendLog(data: LogEntry) { try { - const response = await fetch(`/api/log?id=${data.id}&proof=${data.proof}&error=${data.error || null}`, { - method: 'GET', - }); + const response = await fetch( + `/api/log?id=${data.id}&proof=${data.proof}&merkle_root=${data.merkle_root}&nullifier_hash=${ + data.nullifier_hash + }&error=${data.error || null}`, + { + method: 'GET', + }, + ); if (!response.ok) { throw new Error('Failed to send log data'); From 2126ed8437eb3b787f59150753ba09eaf9bc21fa Mon Sep 17 00:00:00 2001 From: titix Date: Wed, 8 May 2024 15:58:29 -0300 Subject: [PATCH 07/12] fix: typo --- src/utils/logs.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/logs.ts b/src/utils/logs.ts index 7cc9145..9feefe2 100644 --- a/src/utils/logs.ts +++ b/src/utils/logs.ts @@ -9,7 +9,7 @@ interface LogEntry { export async function sendLog(data: LogEntry) { try { const response = await fetch( - `/api/log?id=${data.id}&proof=${data.proof}&merkle_root=${data.merkle_root}&nullifier_hash=${ + `/api/logs?id=${data.id}&proof=${data.proof}&merkle_root=${data.merkle_root}&nullifier_hash=${ data.nullifier_hash }&error=${data.error || null}`, { From 8eccd04f4342671331078f6dd213c7273b09f1ce Mon Sep 17 00:00:00 2001 From: titix Date: Wed, 8 May 2024 16:07:45 -0300 Subject: [PATCH 08/12] fix: merkle and nullhash --- src/pages/api/logs.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/pages/api/logs.ts b/src/pages/api/logs.ts index 92c9084..9284888 100644 --- a/src/pages/api/logs.ts +++ b/src/pages/api/logs.ts @@ -2,8 +2,8 @@ import type { NextApiRequest, NextApiResponse } from 'next'; export default function handler(req: NextApiRequest, res: NextApiResponse) { if (req.method === 'GET') { - const { id, proof, error } = req.query; - const logData = { id, proof, error }; + const { id, merkle_root, nullifier_hash, proof, error } = req.query; + const logData = { id, merkle_root, nullifier_hash, proof, error }; console.log(logData); From 7e4e7ee838f3d0804cf4fd42e5882aaae54dbff5 Mon Sep 17 00:00:00 2001 From: titix Date: Wed, 8 May 2024 17:10:40 -0300 Subject: [PATCH 09/12] fix: post req --- src/pages/api/logs.ts | 6 +++--- src/utils/logs.ts | 16 ++++++---------- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/src/pages/api/logs.ts b/src/pages/api/logs.ts index 9284888..8091bc9 100644 --- a/src/pages/api/logs.ts +++ b/src/pages/api/logs.ts @@ -1,15 +1,15 @@ import type { NextApiRequest, NextApiResponse } from 'next'; export default function handler(req: NextApiRequest, res: NextApiResponse) { - if (req.method === 'GET') { - const { id, merkle_root, nullifier_hash, proof, error } = req.query; + if (req.method === 'POST') { + const { id, merkle_root, nullifier_hash, proof, error } = req.body; const logData = { id, merkle_root, nullifier_hash, proof, error }; console.log(logData); res.status(200).json({ message: 'Log successful', log: logData }); } else { - res.setHeader('Allow', ['GET']); + res.setHeader('Allow', ['POST']); res.status(405).end(`Method ${req.method} Not Allowed`); } } diff --git a/src/utils/logs.ts b/src/utils/logs.ts index 9feefe2..8f69cf2 100644 --- a/src/utils/logs.ts +++ b/src/utils/logs.ts @@ -8,21 +8,17 @@ 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'); } - - const responseData = await response.json(); - console.log('Server log response:', responseData); } catch (error) { console.error('Failed to send log:', error); } From 9c7031cfb5e57a5ee8815cf39a18e2fe23cca5d5 Mon Sep 17 00:00:00 2001 From: titix Date: Wed, 8 May 2024 17:21:52 -0300 Subject: [PATCH 10/12] feat: add cors revert get --- package.json | 1 + pnpm-lock.yaml | 20 ++++++++++++++++++++ src/pages/api/logs.ts | 16 ++++++++++++---- src/utils/logs.ts | 16 ++++++++++------ 4 files changed, 43 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index 4b25799..4ea3372 100644 --- a/package.json +++ b/package.json @@ -44,6 +44,7 @@ "clsx": "2.1.0", "js-confetti": "0.12.0", "next": "14.1.3", + "nextjs-cors": "2.2.0", "react": "18.2.0", "react-dom": "18.2.0", "react-router-dom": "6.15.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 7329927..71241d6 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -50,6 +50,9 @@ dependencies: next: specifier: 14.1.3 version: 14.1.3(@babel/core@7.24.1)(react-dom@18.2.0)(react@18.2.0) + nextjs-cors: + specifier: ^2.2.0 + version: 2.2.0(next@14.1.3) react: specifier: 18.2.0 version: 18.2.0 @@ -5570,6 +5573,14 @@ packages: resolution: {integrity: sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ==} dev: false + /cors@2.8.5: + resolution: {integrity: sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==} + engines: {node: '>= 0.10'} + dependencies: + object-assign: 4.1.1 + vary: 1.1.2 + dev: false + /cosmiconfig@5.2.1: resolution: {integrity: sha512-H65gsXo1SKjf8zmrJ67eJk8aIRKV5ff2D4uKZIBZShbhGSpEmsQOPW/SKMKYhSTrqR7ufy6RP69rPogdaPh/kA==} engines: {node: '>=4'} @@ -8917,6 +8928,15 @@ packages: - babel-plugin-macros dev: false + /nextjs-cors@2.2.0(next@14.1.3): + resolution: {integrity: sha512-FZu/A+L59J4POJNqwXYyCPDvsLDeu5HjSBvytzS6lsrJeDz5cmnH45zV+VoNic0hjaeER9xGaiIjZIWzEHnxQg==} + peerDependencies: + next: ^8.1.1-canary.54 || ^9.0.0 || ^10.0.0-0 || ^11.0.0 || ^12.0.0 || ^13.0.0 || ^14.0.0 + dependencies: + cors: 2.8.5 + next: 14.1.3(@babel/core@7.24.1)(react-dom@18.2.0)(react@18.2.0) + dev: false + /nocache@3.0.4: resolution: {integrity: sha512-WDD0bdg9mbq6F4mRxEYcPWwfA1vxd0mrvKOyxI7Xj/atfRHVeutzuWByG//jfm4uPzp0y4Kj051EORCBSQMycw==} engines: {node: '>=12.0.0'} diff --git a/src/pages/api/logs.ts b/src/pages/api/logs.ts index 8091bc9..e954b5b 100644 --- a/src/pages/api/logs.ts +++ b/src/pages/api/logs.ts @@ -1,15 +1,23 @@ import type { NextApiRequest, NextApiResponse } from 'next'; +import Cors from 'nextjs-cors'; -export default function handler(req: NextApiRequest, res: NextApiResponse) { - if (req.method === 'POST') { - const { id, merkle_root, nullifier_hash, proof, error } = req.body; +export default async function handler(req: NextApiRequest, res: NextApiResponse) { + await Cors(req, res, { + methods: ['GET'], + origin: 'https://www.moregoats.com/', + allowCredentials: true, + optionsSuccessStatus: 200, + }); + + 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 }); } else { - res.setHeader('Allow', ['POST']); + res.setHeader('Allow', ['GET']); res.status(405).end(`Method ${req.method} Not Allowed`); } } diff --git a/src/utils/logs.ts b/src/utils/logs.ts index 8f69cf2..9feefe2 100644 --- a/src/utils/logs.ts +++ b/src/utils/logs.ts @@ -8,17 +8,21 @@ interface LogEntry { export async function sendLog(data: LogEntry) { try { - const response = await fetch('/api/logs', { - method: 'POST', - headers: { - 'Content-Type': 'application/json', + 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', }, - body: JSON.stringify(data), - }); + ); if (!response.ok) { throw new Error('Failed to send log data'); } + + const responseData = await response.json(); + console.log('Server log response:', responseData); } catch (error) { console.error('Failed to send log:', error); } From af4c591d4194f02b33499c67dd1faa2da4a041ff Mon Sep 17 00:00:00 2001 From: titix Date: Wed, 8 May 2024 17:34:57 -0300 Subject: [PATCH 11/12] fix: check origin --- package.json | 1 - pnpm-lock.yaml | 20 -------------------- src/pages/api/logs.ts | 15 ++++++++------- 3 files changed, 8 insertions(+), 28 deletions(-) diff --git a/package.json b/package.json index 4ea3372..4b25799 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,6 @@ "clsx": "2.1.0", "js-confetti": "0.12.0", "next": "14.1.3", - "nextjs-cors": "2.2.0", "react": "18.2.0", "react-dom": "18.2.0", "react-router-dom": "6.15.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 71241d6..7329927 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -50,9 +50,6 @@ dependencies: next: specifier: 14.1.3 version: 14.1.3(@babel/core@7.24.1)(react-dom@18.2.0)(react@18.2.0) - nextjs-cors: - specifier: ^2.2.0 - version: 2.2.0(next@14.1.3) react: specifier: 18.2.0 version: 18.2.0 @@ -5573,14 +5570,6 @@ packages: resolution: {integrity: sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ==} dev: false - /cors@2.8.5: - resolution: {integrity: sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==} - engines: {node: '>= 0.10'} - dependencies: - object-assign: 4.1.1 - vary: 1.1.2 - dev: false - /cosmiconfig@5.2.1: resolution: {integrity: sha512-H65gsXo1SKjf8zmrJ67eJk8aIRKV5ff2D4uKZIBZShbhGSpEmsQOPW/SKMKYhSTrqR7ufy6RP69rPogdaPh/kA==} engines: {node: '>=4'} @@ -8928,15 +8917,6 @@ packages: - babel-plugin-macros dev: false - /nextjs-cors@2.2.0(next@14.1.3): - resolution: {integrity: sha512-FZu/A+L59J4POJNqwXYyCPDvsLDeu5HjSBvytzS6lsrJeDz5cmnH45zV+VoNic0hjaeER9xGaiIjZIWzEHnxQg==} - peerDependencies: - next: ^8.1.1-canary.54 || ^9.0.0 || ^10.0.0-0 || ^11.0.0 || ^12.0.0 || ^13.0.0 || ^14.0.0 - dependencies: - cors: 2.8.5 - next: 14.1.3(@babel/core@7.24.1)(react-dom@18.2.0)(react@18.2.0) - dev: false - /nocache@3.0.4: resolution: {integrity: sha512-WDD0bdg9mbq6F4mRxEYcPWwfA1vxd0mrvKOyxI7Xj/atfRHVeutzuWByG//jfm4uPzp0y4Kj051EORCBSQMycw==} engines: {node: '>=12.0.0'} diff --git a/src/pages/api/logs.ts b/src/pages/api/logs.ts index e954b5b..9769587 100644 --- a/src/pages/api/logs.ts +++ b/src/pages/api/logs.ts @@ -1,13 +1,14 @@ import type { NextApiRequest, NextApiResponse } from 'next'; -import Cors from 'nextjs-cors'; export default async function handler(req: NextApiRequest, res: NextApiResponse) { - await Cors(req, res, { - methods: ['GET'], - origin: 'https://www.moregoats.com/', - allowCredentials: true, - optionsSuccessStatus: 200, - }); + const referer = req.headers.referer; + + const allowedReferers = ['https://www.moregoats.com/', 'https://dev.moregoats.com/']; + + 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; From 3e2d2718c38783f3c8b398f2577e2892c386e5cc Mon Sep 17 00:00:00 2001 From: titix Date: Wed, 8 May 2024 18:01:19 -0300 Subject: [PATCH 12/12] fix: post req --- src/pages/api/logs.ts | 26 +++++++++++--------------- src/utils/logs.ts | 13 ++++++------- 2 files changed, 17 insertions(+), 22 deletions(-) diff --git a/src/pages/api/logs.ts b/src/pages/api/logs.ts index 9769587..6a92091 100644 --- a/src/pages/api/logs.ts +++ b/src/pages/api/logs.ts @@ -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`); } } diff --git a/src/utils/logs.ts b/src/utils/logs.ts index 9feefe2..273ba4d 100644 --- a/src/utils/logs.ts +++ b/src/utils/logs.ts @@ -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');