1
0
mirror of https://github.com/sasjs/server.git synced 2026-01-08 07:00:04 +00:00

chore: added desktop mode + drive tmp folder fix

This commit is contained in:
Saad Jutt
2021-11-12 19:43:56 +05:00
parent 514a262340
commit 2eb42408d1
9 changed files with 58 additions and 12 deletions

View File

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

View 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()
}

View File

@@ -1,3 +1,4 @@
export * from './authenticateToken'
export * from './desktopRestrict'
export * from './verifyAdmin'
export * from './verifyAdminIfNeeded'

View File

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

View File

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

View File

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

View File

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

View File

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