1
0
mirror of https://github.com/sasjs/server.git synced 2025-12-10 11:24:35 +00:00

fix: cors enabled for desktop mode

This commit is contained in:
Saad Jutt
2021-11-13 23:29:27 +05:00
parent cbe07b4abb
commit 2bb10c7166
6 changed files with 47 additions and 28 deletions

13
api/package-lock.json generated
View File

@@ -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",

View File

@@ -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",

View File

@@ -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

View File

@@ -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)

View File

@@ -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)

View File

@@ -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
})