From 2bb10c71661b5de7ed515c82e5b1967b88449972 Mon Sep 17 00:00:00 2001 From: Saad Jutt Date: Sat, 13 Nov 2021 23:29:27 +0500 Subject: [PATCH] fix: cors enabled for desktop mode --- api/package-lock.json | 13 +++++++++++++ api/package.json | 21 +++++++++------------ api/public/swagger.yaml | 4 ++-- api/src/app.ts | 13 +++++++++---- web/src/components/login.tsx | 7 ++++--- web/src/components/useTokens.ts | 17 ++++++++++------- 6 files changed, 47 insertions(+), 28 deletions(-) diff --git a/api/package-lock.json b/api/package-lock.json index ecbd640..3c49fa4 100644 --- a/api/package-lock.json +++ b/api/package-lock.json @@ -27,6 +27,7 @@ }, "devDependencies": { "@types/bcryptjs": "^2.4.2", + "@types/cors": "^2.8.12", "@types/express": "^4.17.12", "@types/jest": "^26.0.24", "@types/jsonwebtoken": "^8.5.5", @@ -2205,6 +2206,12 @@ "integrity": "sha512-t73xJJrvdTjXrn4jLS9VSGRbz0nUY3cl2DMGDU48lKl+HR9dbbjW2A9r3g40VA++mQpy6uuHg33gy7du2BKpog==", "dev": true }, + "node_modules/@types/cors": { + "version": "2.8.12", + "resolved": "https://registry.npmjs.org/@types/cors/-/cors-2.8.12.tgz", + "integrity": "sha512-vt+kDhq/M2ayberEtJcIN/hxXy1Pk+59g2FV/ZQceeaTyCtCucjL2Q7FXlFjtWn4n15KCr1NE2lNNFhp0lEThw==", + "dev": true + }, "node_modules/@types/express": { "version": "4.17.12", "resolved": "https://registry.npmjs.org/@types/express/-/express-4.17.12.tgz", @@ -16651,6 +16658,12 @@ "integrity": "sha512-t73xJJrvdTjXrn4jLS9VSGRbz0nUY3cl2DMGDU48lKl+HR9dbbjW2A9r3g40VA++mQpy6uuHg33gy7du2BKpog==", "dev": true }, + "@types/cors": { + "version": "2.8.12", + "resolved": "https://registry.npmjs.org/@types/cors/-/cors-2.8.12.tgz", + "integrity": "sha512-vt+kDhq/M2ayberEtJcIN/hxXy1Pk+59g2FV/ZQceeaTyCtCucjL2Q7FXlFjtWn4n15KCr1NE2lNNFhp0lEThw==", + "dev": true + }, "@types/express": { "version": "4.17.12", "resolved": "https://registry.npmjs.org/@types/express/-/express-4.17.12.tgz", diff --git a/api/package.json b/api/package.json index e60b350..d31048a 100644 --- a/api/package.json +++ b/api/package.json @@ -15,24 +15,20 @@ "lint:fix": "npx prettier --write \"src/**/*.{ts,tsx,js,jsx,html,css,sass,less,yml,md,graphql}\"", "lint": "npx prettier --check \"src/**/*.{ts,tsx,js,jsx,html,css,sass,less,yml,md,graphql}\"", "package:lib": "npm run build && cp ./package.json build && cp README.md build && cd build && npm version \"5.0.0\" && npm pack", - "exe": "npm run build && cd build && npm run public:copy && npm run web && pkg .", - "public:copy": "cp -r ../public/ ./public/", - "web": "cd .. && npm run web:mkdir && npm run web:copy && cd build", - "web:mkdir": "rimraf web && mkdir web && mkdir web/build", - "web:copy": "cp -r ../web/build/ ./web/build/" + "exe": "npm run build && npm run public:copy && npm run web:copy && pkg .", + "public:copy": "cp -r ./public/ ./build/public/", + "web:copy": "rimraf web && mkdir web && cp -r ../web/build/ ./web/build/" }, - "bin": "src/server.js", + "bin": "./build/src/server.js", "pkg": { "assets": [ - "public/**/*", - "../web/build/**/*" + "./build/public/**/*", + "./web/build/**/*" ], "targets": [ - "node16-linux-x64", - "node16-macos-x64", - "node16-win-x64" + "node16-macos-x64" ], - "outputPath": "../../executables" + "outputPath": "../executables" }, "release": { "branches": [ @@ -57,6 +53,7 @@ }, "devDependencies": { "@types/bcryptjs": "^2.4.2", + "@types/cors": "^2.8.12", "@types/express": "^4.17.12", "@types/jest": "^26.0.24", "@types/jsonwebtoken": "^8.5.5", diff --git a/api/public/swagger.yaml b/api/public/swagger.yaml index c4d4018..3d814f4 100644 --- a/api/public/swagger.yaml +++ b/api/public/swagger.yaml @@ -981,7 +981,7 @@ paths: application/json: schema: type: string - description: "Trigger a SAS program using it's location in the _program parameter.\r\nEnable debugging using the _debug parameter.\r\nAdditional URL parameters are turned into SAS macro variables.\r\nAny files provided are placed into the session and\r\ncorresponding _WEBIN_XXX variables are created." + description: "Trigger a SAS program using it's location in the _program parameter.\nEnable debugging using the _debug parameter.\nAdditional URL parameters are turned into SAS macro variables.\nAny files provided are placed into the session and\ncorresponding _WEBIN_XXX variables are created." summary: 'Execute Stored Program, return raw content' tags: - STP @@ -1005,7 +1005,7 @@ paths: application/json: schema: $ref: '#/components/schemas/ExecuteReturnJsonResponse' - description: "Trigger a SAS program using it's location in the _program parameter.\r\nEnable debugging using the _debug parameter.\r\nAdditional URL parameters are turned into SAS macro variables.\r\nAny files provided are placed into the session and\r\ncorresponding _WEBIN_XXX variables are created." + description: "Trigger a SAS program using it's location in the _program parameter.\nEnable debugging using the _debug parameter.\nAdditional URL parameters are turned into SAS macro variables.\nAny files provided are placed into the session and\ncorresponding _WEBIN_XXX variables are created." summary: 'Execute Stored Program, return JSON' tags: - STP diff --git a/api/src/app.ts b/api/src/app.ts index f4c4b01..59cc320 100644 --- a/api/src/app.ts +++ b/api/src/app.ts @@ -2,15 +2,22 @@ import path from 'path' import express from 'express' import morgan from 'morgan' import dotenv from 'dotenv' +import cors from 'cors' + import webRouter from './routes/web' import apiRouter from './routes/api' import { getWebBuildFolderPath } from './utils' import { connectDB } from './routes/api/auth' +dotenv.config() + const app = express() -const cors=require('cors') -app.use(cors()) +const { MODE } = process.env +if (MODE?.trim() !== 'server') { + console.log('All CORS Requests are enabled') + app.use(cors({ credentials: true, origin: 'http://localhost:3000' })) +} app.use(express.json({ limit: '50mb' })) app.use(morgan('tiny')) @@ -22,6 +29,4 @@ app.use(express.json({ limit: '50mb' })) app.use(express.static(getWebBuildFolderPath())) -dotenv.config() - export default connectDB().then(() => app) diff --git a/web/src/components/login.tsx b/web/src/components/login.tsx index 4ea36c3..0cb8a8e 100644 --- a/web/src/components/login.tsx +++ b/web/src/components/login.tsx @@ -7,16 +7,17 @@ const headers = { Accept: 'application/json', 'Content-Type': 'application/json' } - +const baseUrl = + process.env.NODE_ENV === 'development' ? 'http://localhost:5000' : undefined const getAuthCode = async (credentials: any) => { - return fetch('/SASjsApi/auth/authorize', { + return fetch(`${baseUrl}/SASjsApi/auth/authorize`, { method: 'POST', headers, body: JSON.stringify(credentials) }).then((data) => data.json()) } const getTokens = async (payload: any) => { - return fetch('/SASjsApi/auth/token', { + return fetch(`${baseUrl}/SASjsApi/auth/token`, { method: 'POST', headers, body: JSON.stringify(payload) diff --git a/web/src/components/useTokens.ts b/web/src/components/useTokens.ts index 99f0081..82e2193 100644 --- a/web/src/components/useTokens.ts +++ b/web/src/components/useTokens.ts @@ -43,15 +43,18 @@ export default function useTokens() { } } -// const baseUrl = 'http://localhost:5000' -// const isAbsoluteURLRegex = /^(?:\w+:)\/\// +const baseUrl = + process.env.NODE_ENV === 'development' ? 'http://localhost:5000' : undefined + +const isAbsoluteURLRegex = /^(?:\w+:)\/\// const setAxiosRequestHeader = (accessToken: string) => { - axios.interceptors.request.use(function (config: any) { - // if (!isAbsoluteURLRegex.test(config.url)) { - // config.url = baseUrl + config.url - // } - config.headers.Authorization = `Bearer ${accessToken}` + axios.interceptors.request.use(function (config) { + if (baseUrl && !isAbsoluteURLRegex.test(config.url as string)) { + config.url = baseUrl + config.url + } + config.headers!['Authorization'] = `Bearer ${accessToken}` + config.withCredentials = true return config })