From c65e491f5403348f2d74c4d28b9f9b74f3c625c1 Mon Sep 17 00:00:00 2001 From: gllm-dev Date: Fri, 18 Oct 2024 12:59:01 +0200 Subject: [PATCH] fix: cors --- backend/package.json | 2 ++ backend/src/api/create-encryption-session.ts | 4 ---- backend/src/server.ts | 4 ++-- backend/yarn.lock | 22 +++++++++++++++++- frontend/package.json | 1 + frontend/src/services/openfortService.ts | 24 +++++++++----------- frontend/yarn.lock | 11 ++++++++- 7 files changed, 47 insertions(+), 21 deletions(-) diff --git a/backend/package.json b/backend/package.json index 938a288..0b50545 100644 --- a/backend/package.json +++ b/backend/package.json @@ -16,10 +16,12 @@ "@openfort/openfort-node": "^0.6.66", "@types/express": "^5.0.0", "@types/node": "^22.7.6", + "cors": "^2.8.5", "dotenv": "^16.4.5", "express": "^4.21.1" }, "devDependencies": { + "@types/cors": "^2.8.17", "ts-node-dev": "^2.0.0", "typescript": "^5.6.3" } diff --git a/backend/src/api/create-encryption-session.ts b/backend/src/api/create-encryption-session.ts index aab30cf..64011e1 100644 --- a/backend/src/api/create-encryption-session.ts +++ b/backend/src/api/create-encryption-session.ts @@ -5,11 +5,7 @@ export async function createEncryptionSession( req: Request, res: Response ) { - console.log('createEncryptionSession'); try { - console.log(process.env.SHIELD_API_KEY); - console.log(process.env.SHIELD_SECRET_KEY); - console.log(process.env.SHIELD_ENCRYPTION_SHARE); const session = await openfort.registerRecoverySession(process.env.SHIELD_API_KEY!, process.env.SHIELD_SECRET_KEY!, process.env.SHIELD_ENCRYPTION_SHARE!) res.status(200).send({ session: session, diff --git a/backend/src/server.ts b/backend/src/server.ts index afd0813..3d5f4f3 100644 --- a/backend/src/server.ts +++ b/backend/src/server.ts @@ -1,16 +1,16 @@ import dotenv from 'dotenv'; dotenv.config(); - import express, { Request, Response } from 'express'; import {createEncryptionSession} from "./api/create-encryption-session"; import {createSession} from "./api/create-session"; import {collect} from "./api/collect"; import {revokeSession} from "./api/revoke-session"; - +import cors from 'cors'; // Create an instance of Express const app = express(); app.use(express.json()); +app.use(cors()); // Define the port number const PORT = process.env.SERVER_PORT || 3005; diff --git a/backend/yarn.lock b/backend/yarn.lock index b9e5a0b..04f94c0 100644 --- a/backend/yarn.lock +++ b/backend/yarn.lock @@ -81,6 +81,13 @@ dependencies: "@types/node" "*" +"@types/cors@^2.8.17": + version "2.8.17" + resolved "https://registry.yarnpkg.com/@types/cors/-/cors-2.8.17.tgz#5d718a5e494a8166f569d986794e49c48b216b2b" + integrity sha512-8CGDvrBj1zgo2qE+oS3pOCyYNqCPryMWY2bGfwA0dcfopWGgxs+78df0Rs3rc9THP4JkOhLsAa+15VdpAqkcUA== + dependencies: + "@types/node" "*" + "@types/express-serve-static-core@^5.0.0": version "5.0.0" resolved "https://registry.yarnpkg.com/@types/express-serve-static-core/-/express-serve-static-core-5.0.0.tgz#91f06cda1049e8f17eeab364798ed79c97488a1c" @@ -333,6 +340,14 @@ cookie@0.7.1: resolved "https://registry.yarnpkg.com/cookie/-/cookie-0.7.1.tgz#2f73c42142d5d5cf71310a74fc4ae61670e5dbc9" integrity sha512-6DnInpx7SJ2AK3+CTUE/ZM0vWTUboZCegxhC2xiIydHR9jNuTAASBrfEpHhiGOZw/nX51bHt6YQl8jsGo4y/0w== +cors@^2.8.5: + version "2.8.5" + resolved "https://registry.yarnpkg.com/cors/-/cors-2.8.5.tgz#eac11da51592dd86b9f06f6e7ac293b3df875d29" + integrity sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g== + dependencies: + object-assign "^4" + vary "^1" + create-require@^1.1.0: version "1.1.1" resolved "https://registry.yarnpkg.com/create-require/-/create-require-1.1.1.tgz#c1d7e8f1e5f6cfc9ff65f9cd352d37348756c333" @@ -771,6 +786,11 @@ normalize-path@^3.0.0, normalize-path@~3.0.0: resolved "https://registry.yarnpkg.com/normalize-path/-/normalize-path-3.0.0.tgz#0dcd69ff23a1c9b11fd0978316644a0388216a65" integrity sha512-6eZs5Ls3WtCisHWp9S2GUy8dqkpGi4BVSz3GaqiE6ezub0512ESztXUwUB6C6IKbQkY2Pnb/mD4WYojCRwcwLA== +object-assign@^4: + version "4.1.1" + resolved "https://registry.yarnpkg.com/object-assign/-/object-assign-4.1.1.tgz#2109adc7965887cfc05cbbd442cac8bfbb360863" + integrity sha512-rJgTQnkUnH1sFw8yT6VSU3zD3sWmu6sZhIseY8VX+GRu3P6F7Fu+JNDoXfklElbLJSnc3FUQHVe4cU5hj+BcUg== + object-inspect@^1.13.1: version "1.13.2" resolved "https://registry.yarnpkg.com/object-inspect/-/object-inspect-1.13.2.tgz#dea0088467fb991e67af4058147a24824a3043ff" @@ -1105,7 +1125,7 @@ v8-compile-cache-lib@^3.0.1: resolved "https://registry.yarnpkg.com/v8-compile-cache-lib/-/v8-compile-cache-lib-3.0.1.tgz#6336e8d71965cb3d35a1bbb7868445a7c05264bf" integrity sha512-wa7YjyUGfNZngI/vtK0UHAN+lgDCxBPCylVXGp0zu59Fz5aiGtNXaq3DhIov063MorB+VfufLh3JlF2KdTK3xg== -vary@~1.1.2: +vary@^1, vary@~1.1.2: version "1.1.2" resolved "https://registry.yarnpkg.com/vary/-/vary-1.1.2.tgz#2299f02c6ded30d4a5961b0b9f74524a18f634fc" integrity sha512-BNGbWLfd0eUPabhkXUVm0j8uuvREyTh5ovRa/dyow/BqAbZJyC+5fU+IzQOzmAKzYqYRAISoRhdQr3eIZ/PXqg== diff --git a/frontend/package.json b/frontend/package.json index 39d85d0..5908e0e 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -14,6 +14,7 @@ "@openfort/openfort-node": "^0.6.66", "@telegram-apps/sdk-react": "^2.0.5", "@telegram-apps/telegram-ui": "^2.1.8", + "axios": "^1.7.7", "buffer": "^6.0.3", "eruda": "^3.4.0", "express": "^4.21.1", diff --git a/frontend/src/services/openfortService.ts b/frontend/src/services/openfortService.ts index ee4898c..7a707e2 100644 --- a/frontend/src/services/openfortService.ts +++ b/frontend/src/services/openfortService.ts @@ -9,6 +9,7 @@ import { TypedDataField } from '@openfort/openfort-js'; import openfort from '../utils/openfortConfig'; +import axios from 'axios'; const chainId = Number(import.meta.env.VITE_CHAIN_ID); @@ -32,19 +33,17 @@ class OpenfortService { } async mintNFT(identityToken: string): Promise { try { - const collectResponse = await fetch(`${import.meta.env.VITE_BACKEND_URL}/api/protected-collect`, { - method: "POST", + const collectResponse = await axios.post(`${import.meta.env.VITE_BACKEND_URL}/api/protected-collect`, {}, { headers: { "Content-Type": "application/json", Authorization: `Bearer ${identityToken}`, }, }); - - if (!collectResponse.ok) { + if (collectResponse.status >= 400) { alert("Failed to mint NFT status: " + collectResponse.status); return null } - const collectResponseJSON = await collectResponse.json(); + const collectResponseJSON = await collectResponse.data.json(); if (collectResponseJSON.data?.nextAction) { const response = await openfort.sendSignatureTransactionIntentRequest( @@ -95,18 +94,17 @@ class OpenfortService { async getEncryptionSession(): Promise { console.log('Creating encryption session'); console.log(import.meta.env.VITE_BACKEND_URL) - const resp = await fetch(`${import.meta.env.VITE_BACKEND_URL}/api/protected-create-encryption-session`, { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - }); + const resp = await axios.post(`${import.meta.env.VITE_BACKEND_URL}/api/protected-create-encryption-session`,{}, { + headers: { + "Content-Type": "application/json", + }, + }); - if (!resp.ok) { + if (resp.status >= 400) { throw new Error("Failed to create encryption session"); } - const respJSON = await resp.json(); + const respJSON = await resp.data.json(); return respJSON.session; } diff --git a/frontend/yarn.lock b/frontend/yarn.lock index f1698b8..3fffe3f 100644 --- a/frontend/yarn.lock +++ b/frontend/yarn.lock @@ -1525,6 +1525,15 @@ axios@1.6.7: form-data "^4.0.0" proxy-from-env "^1.1.0" +axios@^1.7.7: + version "1.7.7" + resolved "https://registry.yarnpkg.com/axios/-/axios-1.7.7.tgz#2f554296f9892a72ac8d8e4c5b79c14a91d0a47f" + integrity sha512-S4kL7XrjgBmvdGut0sN3yJxqYzrDOnivkBiN0OFs6hLiUam3UPvswUo0kqGyhqUZGEOytHyumEdXsAkgCOUf3Q== + dependencies: + follow-redirects "^1.15.6" + form-data "^4.0.0" + proxy-from-env "^1.1.0" + balanced-match@^1.0.0: version "1.0.2" resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.2.tgz#e83e3a7e3f300b34cb9d87f615fa0cbf357690ee" @@ -2384,7 +2393,7 @@ flatted@^3.2.9: resolved "https://registry.yarnpkg.com/flatted/-/flatted-3.3.1.tgz#21db470729a6734d4997002f439cb308987f567a" integrity sha512-X8cqMLLie7KsNUDSdzeN8FYK9rEt4Dt67OsG/DNGnYTSDBG4uFAJFBnUeiV+zCVAvwFy56IjM9sH51jVaEhNxw== -follow-redirects@^1.15.4: +follow-redirects@^1.15.4, follow-redirects@^1.15.6: version "1.15.9" resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.9.tgz#a604fa10e443bf98ca94228d9eebcc2e8a2c8ee1" integrity sha512-gew4GsXizNgdoRyqmyfMHyAmXsZDk6mHkSxZFCzW9gwlbtOW44CDtYavM+y+72qD/Vq2l550kMF52DT8fOLJqQ==