feat: db uses asynclocalstorage set schema
feat: set user input related header in public route requests to identify schema fix: small fixes
This commit is contained in:
parent
7135a03654
commit
990ab32583
10 changed files with 58 additions and 15 deletions
|
|
@ -2,9 +2,7 @@ const { asyncLocalStorage, defaultStore } = require('./async-storage')
|
|||
|
||||
const computeSchema = (req, res, next) => {
|
||||
const store = defaultStore()
|
||||
asyncLocalStorage.run(store, () => {
|
||||
next()
|
||||
})
|
||||
return asyncLocalStorage.run(store, () => next())
|
||||
}
|
||||
|
||||
module.exports = computeSchema
|
||||
|
|
|
|||
|
|
@ -38,7 +38,10 @@ const _task = (obj, opts, cb) => {
|
|||
})
|
||||
}
|
||||
|
||||
const getSchema = () => 'public'
|
||||
const getSchema = () => {
|
||||
const store = asyncLocalStorage.getStore() ?? defaultStore()
|
||||
return asyncLocalStorage.run(store, () => store.get('schema'))
|
||||
}
|
||||
const getDefaultSchema = () => 'ERROR_SCHEMA'
|
||||
|
||||
const searchPathWrapper = (t, cb) => {
|
||||
|
|
|
|||
|
|
@ -44,8 +44,9 @@ app.use(express.json())
|
|||
app.use(express.urlencoded({ extended: true })) // support encoded bodies
|
||||
app.use(express.static(path.resolve(__dirname, '..', '..', 'public')))
|
||||
app.use(cleanUserSessions(USER_SESSIONS_CLEAR_INTERVAL))
|
||||
app.use(session)
|
||||
app.use(computeSchema)
|
||||
app.use(findOperatorId)
|
||||
app.use(session)
|
||||
|
||||
const apolloServer = new ApolloServer({
|
||||
typeDefs,
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
const users = require('../../users')
|
||||
|
||||
const buildApolloContext = async ({ req, res }) => {
|
||||
if (!req.session.user) return { req }
|
||||
if (!req.session.user) return { req, res }
|
||||
|
||||
const user = await users.verifyAndUpdateUser(
|
||||
req.session.user.id,
|
||||
|
|
@ -14,12 +14,15 @@ const buildApolloContext = async ({ req, res }) => {
|
|||
req.session.ipAddress = req.ip
|
||||
req.session.lastUsed = new Date(Date.now()).toISOString()
|
||||
req.session.user.id = user.id
|
||||
req.session.user.username = user.username
|
||||
req.session.user.role = user.role
|
||||
|
||||
|
||||
res.set('role', user.role)
|
||||
res.cookie('email', user.username)
|
||||
res.set('Access-Control-Expose-Headers', 'role')
|
||||
|
||||
return { req }
|
||||
return { req, res }
|
||||
}
|
||||
|
||||
module.exports = buildApolloContext
|
||||
|
|
|
|||
|
|
@ -31,7 +31,6 @@ const verifyTxRoutes = require('./routes/verifyTxRoutes')
|
|||
const verifyPromoCodeRoutes = require('./routes/verifyPromoCodeRoutes')
|
||||
|
||||
const app = express()
|
||||
const localApp = express()
|
||||
|
||||
const configRequiredRoutes = [
|
||||
'/poll',
|
||||
|
|
@ -85,4 +84,4 @@ app.use((req, res) => {
|
|||
res.status(404).json({ error: 'No such route' })
|
||||
})
|
||||
|
||||
module.exports = { app, localApp }
|
||||
module.exports = { app }
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue