mirror of
https://github.com/sasjs/server.git
synced 2025-12-10 19:34:34 +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": "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:prepare": "npm run build && cd build && npm run api && npm run public:copy && npm run web && pkg .",
|
||||
"api": "npm run api:mkdir && npm run api:copy",
|
||||
"api:mkdir": "mkdir tmp && mkdir tmp/files && mkdir tmp/logs && mkdir tmp/webouts && mkdir tmp/sessions",
|
||||
"api:copy": "cp -r ../tmp/ ./tmp/",
|
||||
"exe": "npm run exe:prepare && npm run exe:run",
|
||||
"exe:prepare": "npm run build && cd build && npm run public:copy && npm run web && pkg .",
|
||||
"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",
|
||||
"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",
|
||||
@@ -27,7 +26,6 @@
|
||||
"bin": "src/server.js",
|
||||
"pkg": {
|
||||
"assets": [
|
||||
"tmp/files/**/*",
|
||||
"public/**/*",
|
||||
"../web/build/**/*"
|
||||
],
|
||||
|
||||
@@ -28,6 +28,9 @@ const authenticateToken = (
|
||||
key: string,
|
||||
tokenType: 'accessToken' | 'refreshToken' = 'accessToken'
|
||||
) => {
|
||||
const { MODE } = process.env
|
||||
if (MODE === 'desktop') return next()
|
||||
|
||||
const authHeader = req.headers['authorization']
|
||||
const token = authHeader?.split(' ')[1]
|
||||
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 './desktopRestrict'
|
||||
export * from './verifyAdmin'
|
||||
export * from './verifyAdminIfNeeded'
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
export const verifyAdmin = (req: any, res: any, next: any) => {
|
||||
const { MODE } = process.env
|
||||
if (MODE === 'desktop') return next()
|
||||
|
||||
const { user } = req
|
||||
if (!user?.isAdmin) return res.status(401).send('Admin account required')
|
||||
next()
|
||||
|
||||
@@ -25,6 +25,12 @@ export const populateClients = async () => {
|
||||
}
|
||||
|
||||
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
|
||||
// we should exlcude connecting to the real database
|
||||
if (process.env.NODE_ENV !== 'test') {
|
||||
|
||||
@@ -2,7 +2,11 @@ import express from 'express'
|
||||
import dotenv from 'dotenv'
|
||||
import swaggerUi from 'swagger-ui-express'
|
||||
|
||||
import { authenticateAccessToken, verifyAdmin } from '../../middlewares'
|
||||
import {
|
||||
authenticateAccessToken,
|
||||
desktopRestrict,
|
||||
verifyAdmin
|
||||
} from '../../middlewares'
|
||||
|
||||
import driveRouter from './drive'
|
||||
import stpRouter from './stp'
|
||||
@@ -16,12 +20,18 @@ connectDB()
|
||||
|
||||
const router = express.Router()
|
||||
|
||||
router.use('/auth', authRouter)
|
||||
router.use('/client', authenticateAccessToken, verifyAdmin, clientRouter)
|
||||
router.use('/auth', desktopRestrict, authRouter)
|
||||
router.use(
|
||||
'/client',
|
||||
desktopRestrict,
|
||||
authenticateAccessToken,
|
||||
verifyAdmin,
|
||||
clientRouter
|
||||
)
|
||||
router.use('/drive', authenticateAccessToken, driveRouter)
|
||||
router.use('/group', groupRouter)
|
||||
router.use('/group', desktopRestrict, groupRouter)
|
||||
router.use('/stp', authenticateAccessToken, stpRouter)
|
||||
router.use('/user', userRouter)
|
||||
router.use('/user', desktopRestrict, userRouter)
|
||||
router.use(
|
||||
'/',
|
||||
swaggerUi.serve,
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { readFile } from '@sasjs/utils'
|
||||
import express from 'express'
|
||||
import path from 'path'
|
||||
import { getWebBuildFolderPath } from '../../utils'
|
||||
@@ -5,7 +6,24 @@ import { getWebBuildFolderPath } from '../../utils'
|
||||
const webRouter = express.Router()
|
||||
|
||||
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
|
||||
|
||||
@@ -5,7 +5,7 @@ export const getWebBuildFolderPath = () =>
|
||||
getRealPath(path.join(__dirname, '..', '..', '..', 'web', 'build'))
|
||||
|
||||
export const getTmpFolderPath = () =>
|
||||
getRealPath(path.join(__dirname, '..', '..', 'tmp'))
|
||||
getRealPath(path.join(process.cwd(), 'tmp'))
|
||||
|
||||
export const getTmpFilesFolderPath = () =>
|
||||
path.join(getTmpFolderPath(), 'files')
|
||||
|
||||
Reference in New Issue
Block a user