OpenAPITools / OpenAPITools/openapi-generator
[NodeJS] Open API generator for nodejs with middleware (how to implement it nicely)
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Is your feature request related to a problem? Please describe.
In the generated code of NodeJS Server, is there a way we can implement a middleware?
Describe the solution you'd like
I want to implement a middleware that intercepts requests after openapi router successfully validates the request, it will only run on specific controllers ie. JWT validate base on stored tokens in MongoDB like the ones we do on normal nodejs server:
route
router.get('/users/me', auth, async(req, res) => {
// View logged in user profile
res.send(req.user)
})
auth
const jwt = require('jsonwebtoken')
const User = require('../models/User')
const auth = async(req, res, next) => {
const token = req.header('Authorization').replace('Bearer ', '')
const data = jwt.verify(token, process.env.JWT_KEY)
try {
const user = await User.findOne({ _id: data._id, 'tokens.token': token })
if (!user) {
throw new Error()
}
req.user = user
req.token = token
next()
} catch (error) {
res.status(401).send({ error: 'Not authorized to access this resource' })
}
}
module.exports = auth
Describe alternatives you've considered
I could create a service to manually call the verification of token on every controller/service that i want it to be validated but I think it is redundant and not really clean
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
No files or tests are named. Start by locating the NodeJS server generator and its generated OpenAPI router, then trace how requests reach controllers and services. Done would require a decided, documented approach for applying authentication middleware after validation to selected controllers.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- mongodb, node.js
- Domain
- api, authentication, backend
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100