mirror of
https://github.com/sasjs/server.git
synced 2026-01-15 09:50:06 +00:00
chore: added desktop mode + drive tmp folder fix
This commit is contained in:
@@ -15,10 +15,9 @@
|
|||||||
"lint:fix": "npx prettier --write \"src/**/*.{ts,tsx,js,jsx,html,css,sass,less,yml,md,graphql}\"",
|
"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}\"",
|
"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",
|
"package:lib": "npm run build && cp ./package.json build && cp README.md build && cd build && npm version \"5.0.0\" && npm pack",
|
||||||
"exe:prepare": "npm run build && cd build && npm run api && npm run public:copy && npm run web && pkg .",
|
"exe": "npm run exe:prepare && npm run exe:run",
|
||||||
"api": "npm run api:mkdir && npm run api:copy",
|
"exe:prepare": "npm run build && cd build && npm run public:copy && npm run web && pkg .",
|
||||||
"api:mkdir": "mkdir tmp && mkdir tmp/files && mkdir tmp/logs && mkdir tmp/webouts && mkdir tmp/sessions",
|
"exe:run": "cd build && ACCESS_TOKEN_SECRET=123 REFRESH_TOKEN_SECRET=456 AUTH_CODE_SECRET=789 DB_CONNECT=\"mongodb+srv://deved:69OFYcgJ1r3Z8ilN@cluster0.hj4h5.mongodb.net/sasjs_server?retryWrites=true&w=majority\" ./dist/api",
|
||||||
"api:copy": "cp -r ../tmp/ ./tmp/",
|
|
||||||
"public:copy": "cp -r ../public/ ./public/",
|
"public:copy": "cp -r ../public/ ./public/",
|
||||||
"web": "cd .. && npm run web:mkdir && npm run web:copy && cd build",
|
"web": "cd .. && npm run web:mkdir && npm run web:copy && cd build",
|
||||||
"web:mkdir": "rimraf web && mkdir web && mkdir web/build",
|
"web:mkdir": "rimraf web && mkdir web && mkdir web/build",
|
||||||
@@ -27,7 +26,6 @@
|
|||||||
"bin": "src/server.js",
|
"bin": "src/server.js",
|
||||||
"pkg": {
|
"pkg": {
|
||||||
"assets": [
|
"assets": [
|
||||||
"tmp/files/**/*",
|
|
||||||
"public/**/*",
|
"public/**/*",
|
||||||
"../web/build/**/*"
|
"../web/build/**/*"
|
||||||
],
|
],
|
||||||
|
|||||||
@@ -28,6 +28,9 @@ const authenticateToken = (
|
|||||||
key: string,
|
key: string,
|
||||||
tokenType: 'accessToken' | 'refreshToken' = 'accessToken'
|
tokenType: 'accessToken' | 'refreshToken' = 'accessToken'
|
||||||
) => {
|
) => {
|
||||||
|
const { MODE } = process.env
|
||||||
|
if (MODE === 'desktop') return next()
|
||||||
|
|
||||||
const authHeader = req.headers['authorization']
|
const authHeader = req.headers['authorization']
|
||||||
const token = authHeader?.split(' ')[1]
|
const token = authHeader?.split(' ')[1]
|
||||||
if (!token) return res.sendStatus(401)
|
if (!token) return res.sendStatus(401)
|
||||||
|
|||||||
7
api/src/middlewares/desktopRestrict.ts
Normal file
7
api/src/middlewares/desktopRestrict.ts
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
export const desktopRestrict = (req: any, res: any, next: any) => {
|
||||||
|
const { MODE } = process.env
|
||||||
|
if (MODE === 'desktop')
|
||||||
|
return res.status(403).send('Not Allowed while in Desktop Mode.')
|
||||||
|
|
||||||
|
next()
|
||||||
|
}
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
export * from './authenticateToken'
|
export * from './authenticateToken'
|
||||||
|
export * from './desktopRestrict'
|
||||||
export * from './verifyAdmin'
|
export * from './verifyAdmin'
|
||||||
export * from './verifyAdminIfNeeded'
|
export * from './verifyAdminIfNeeded'
|
||||||
|
|||||||
@@ -1,4 +1,7 @@
|
|||||||
export const verifyAdmin = (req: any, res: any, next: any) => {
|
export const verifyAdmin = (req: any, res: any, next: any) => {
|
||||||
|
const { MODE } = process.env
|
||||||
|
if (MODE === 'desktop') return next()
|
||||||
|
|
||||||
const { user } = req
|
const { user } = req
|
||||||
if (!user?.isAdmin) return res.status(401).send('Admin account required')
|
if (!user?.isAdmin) return res.status(401).send('Admin account required')
|
||||||
next()
|
next()
|
||||||
|
|||||||
@@ -25,6 +25,12 @@ export const populateClients = async () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const connectDB = () => {
|
export const connectDB = () => {
|
||||||
|
const { MODE } = process.env
|
||||||
|
if (MODE === 'desktop') {
|
||||||
|
console.log('Running in Destop Mode, no DB to connect.')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// NOTE: when exporting app.js as agent for supertest
|
// NOTE: when exporting app.js as agent for supertest
|
||||||
// we should exlcude connecting to the real database
|
// we should exlcude connecting to the real database
|
||||||
if (process.env.NODE_ENV !== 'test') {
|
if (process.env.NODE_ENV !== 'test') {
|
||||||
|
|||||||
@@ -2,7 +2,11 @@ import express from 'express'
|
|||||||
import dotenv from 'dotenv'
|
import dotenv from 'dotenv'
|
||||||
import swaggerUi from 'swagger-ui-express'
|
import swaggerUi from 'swagger-ui-express'
|
||||||
|
|
||||||
import { authenticateAccessToken, verifyAdmin } from '../../middlewares'
|
import {
|
||||||
|
authenticateAccessToken,
|
||||||
|
desktopRestrict,
|
||||||
|
verifyAdmin
|
||||||
|
} from '../../middlewares'
|
||||||
|
|
||||||
import driveRouter from './drive'
|
import driveRouter from './drive'
|
||||||
import stpRouter from './stp'
|
import stpRouter from './stp'
|
||||||
@@ -16,12 +20,18 @@ connectDB()
|
|||||||
|
|
||||||
const router = express.Router()
|
const router = express.Router()
|
||||||
|
|
||||||
router.use('/auth', authRouter)
|
router.use('/auth', desktopRestrict, authRouter)
|
||||||
router.use('/client', authenticateAccessToken, verifyAdmin, clientRouter)
|
router.use(
|
||||||
|
'/client',
|
||||||
|
desktopRestrict,
|
||||||
|
authenticateAccessToken,
|
||||||
|
verifyAdmin,
|
||||||
|
clientRouter
|
||||||
|
)
|
||||||
router.use('/drive', authenticateAccessToken, driveRouter)
|
router.use('/drive', authenticateAccessToken, driveRouter)
|
||||||
router.use('/group', groupRouter)
|
router.use('/group', desktopRestrict, groupRouter)
|
||||||
router.use('/stp', authenticateAccessToken, stpRouter)
|
router.use('/stp', authenticateAccessToken, stpRouter)
|
||||||
router.use('/user', userRouter)
|
router.use('/user', desktopRestrict, userRouter)
|
||||||
router.use(
|
router.use(
|
||||||
'/',
|
'/',
|
||||||
swaggerUi.serve,
|
swaggerUi.serve,
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import { readFile } from '@sasjs/utils'
|
||||||
import express from 'express'
|
import express from 'express'
|
||||||
import path from 'path'
|
import path from 'path'
|
||||||
import { getWebBuildFolderPath } from '../../utils'
|
import { getWebBuildFolderPath } from '../../utils'
|
||||||
@@ -5,7 +6,24 @@ import { getWebBuildFolderPath } from '../../utils'
|
|||||||
const webRouter = express.Router()
|
const webRouter = express.Router()
|
||||||
|
|
||||||
webRouter.get('/', async (_, res) => {
|
webRouter.get('/', async (_, res) => {
|
||||||
res.sendFile(path.join(getWebBuildFolderPath(), 'index.html'))
|
const indexHtmlPath = path.join(getWebBuildFolderPath(), 'index.html')
|
||||||
|
|
||||||
|
const { MODE } = process.env
|
||||||
|
if (MODE === 'desktop') {
|
||||||
|
const content = await readFile(indexHtmlPath)
|
||||||
|
|
||||||
|
const codeToInject = `
|
||||||
|
<script>
|
||||||
|
localStorage.setItem('accessToken', JSON.stringify('accessToken'))
|
||||||
|
localStorage.setItem('refreshToken', JSON.stringify('refreshToken'))
|
||||||
|
</script>`
|
||||||
|
const injectedContent = content.replace('</head>', `${codeToInject}</head>`)
|
||||||
|
|
||||||
|
res.setHeader('Content-Type', 'text/html')
|
||||||
|
return res.send(injectedContent)
|
||||||
|
}
|
||||||
|
|
||||||
|
res.sendFile(indexHtmlPath)
|
||||||
})
|
})
|
||||||
|
|
||||||
export default webRouter
|
export default webRouter
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ export const getWebBuildFolderPath = () =>
|
|||||||
getRealPath(path.join(__dirname, '..', '..', '..', 'web', 'build'))
|
getRealPath(path.join(__dirname, '..', '..', '..', 'web', 'build'))
|
||||||
|
|
||||||
export const getTmpFolderPath = () =>
|
export const getTmpFolderPath = () =>
|
||||||
getRealPath(path.join(__dirname, '..', '..', 'tmp'))
|
getRealPath(path.join(process.cwd(), 'tmp'))
|
||||||
|
|
||||||
export const getTmpFilesFolderPath = () =>
|
export const getTmpFilesFolderPath = () =>
|
||||||
path.join(getTmpFolderPath(), 'files')
|
path.join(getTmpFolderPath(), 'files')
|
||||||
|
|||||||
Reference in New Issue
Block a user