Skip to content

Commit

Permalink
fix: using cors
Browse files Browse the repository at this point in the history
  • Loading branch information
jamalavedra committed Aug 7, 2024
1 parent fa295a5 commit 4fa2f8f
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 23 deletions.
2 changes: 2 additions & 0 deletions examples/apps/auth-sample/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"@tanstack/react-query": "^5.28.4",
"axios": "^1.6.8",
"clsx": "^1.2.1",
"cors": "^2.8.5",
"ethers": "5.7.2",
"next": "^12.3.1",
"react": "^18.3.0",
Expand All @@ -26,6 +27,7 @@
"wagmi": "^2.9.2"
},
"devDependencies": {
"@types/cors": "^2.8.17",
"@types/react": "^18.3.0",
"autoprefixer": "^10.3.4",
"eslint": "7.32.0",
Expand Down
22 changes: 0 additions & 22 deletions examples/apps/auth-sample/src/middleware.ts

This file was deleted.

3 changes: 3 additions & 0 deletions examples/apps/auth-sample/src/pages/api/protected-collect.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { NextApiRequest, NextApiResponse } from 'next';
import openfort from '../../utils/openfortAdminConfig';
import cors, { runMiddleware } from '../../utils/cors';

const policy_id = 'pol_e7491b89-528e-40bb-b3c2-9d40afa4fefc';
const contract_id = 'con_8d6b19e8-3a5a-4643-8dee-778997a7dffc';
Expand All @@ -10,6 +11,8 @@ export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
await runMiddleware(req, res, cors);

const accessToken = req.headers.authorization?.split(' ')[1];
if (!accessToken) {
return res.status(401).send({
Expand Down
22 changes: 22 additions & 0 deletions examples/apps/auth-sample/src/utils/cors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { NextApiRequest, NextApiResponse } from 'next';
import Cors from "cors"

const cors = Cors({
methods: ['GET', 'OPTIONS', 'POST', 'PUT', 'PATCH', 'DELETE'],
origin: '*',
credentials: true,
});

function runMiddleware(req: NextApiRequest, res: NextApiResponse, fn: Function) {
return new Promise((resolve, reject) => {
fn(req, res, (result: any) => {
if (result instanceof Error) {
return reject(result);
}
return resolve(result);
});
});
}

export default cors;
export { runMiddleware };
30 changes: 29 additions & 1 deletion examples/apps/auth-sample/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1964,6 +1964,15 @@ __metadata:
languageName: node
linkType: hard

"@types/cors@npm:^2.8.17":
version: 2.8.17
resolution: "@types/cors@npm:2.8.17"
dependencies:
"@types/node": "*"
checksum: 469bd85e29a35977099a3745c78e489916011169a664e97c4c3d6538143b0a16e4cc72b05b407dc008df3892ed7bf595f9b7c0f1f4680e169565ee9d64966bde
languageName: node
linkType: hard

"@types/debug@npm:^4.1.7":
version: 4.1.12
resolution: "@types/debug@npm:4.1.12"
Expand Down Expand Up @@ -2920,10 +2929,12 @@ __metadata:
"@rainbow-me/rainbowkit": ^2.1.1
"@tailwindcss/forms": ^0.5.3
"@tanstack/react-query": ^5.28.4
"@types/cors": ^2.8.17
"@types/react": ^18.3.0
autoprefixer: ^10.3.4
axios: ^1.6.8
clsx: ^1.2.1
cors: ^2.8.5
eslint: 7.32.0
eslint-config-airbnb: ^19.0.4
eslint-config-import: ^0.13.0
Expand Down Expand Up @@ -3532,6 +3543,16 @@ __metadata:
languageName: node
linkType: hard

"cors@npm:^2.8.5":
version: 2.8.5
resolution: "cors@npm:2.8.5"
dependencies:
object-assign: ^4
vary: ^1
checksum: ced838404ccd184f61ab4fdc5847035b681c90db7ac17e428f3d81d69e2989d2b680cc254da0e2554f5ed4f8a341820a1ce3d1c16b499f6e2f47a1b9b07b5006
languageName: node
linkType: hard

"crc-32@npm:^1.2.0":
version: 1.2.2
resolution: "crc-32@npm:1.2.2"
Expand Down Expand Up @@ -6993,7 +7014,7 @@ __metadata:
languageName: node
linkType: hard

"object-assign@npm:^4.0.1, object-assign@npm:^4.1.1":
"object-assign@npm:^4, object-assign@npm:^4.0.1, object-assign@npm:^4.1.1":
version: 4.1.1
resolution: "object-assign@npm:4.1.1"
checksum: fcc6e4ea8c7fe48abfbb552578b1c53e0d194086e2e6bbbf59e0a536381a292f39943c6e9628af05b5528aa5e3318bb30d6b2e53cadaf5b8fe9e12c4b69af23f
Expand Down Expand Up @@ -9368,6 +9389,13 @@ __metadata:
languageName: node
linkType: hard

"vary@npm:^1":
version: 1.1.2
resolution: "vary@npm:1.1.2"
checksum: ae0123222c6df65b437669d63dfa8c36cee20a504101b2fcd97b8bf76f91259c17f9f2b4d70a1e3c6bbcee7f51b28392833adb6b2770b23b01abec84e369660b
languageName: node
linkType: hard

"viem@npm:2.9.31":
version: 2.9.31
resolution: "viem@npm:2.9.31"
Expand Down

0 comments on commit 4fa2f8f

Please sign in to comment.