How to add second piece of middleware into API controller structure? [closed]

3 weeks ago 15
ARTICLE AD BOX

I have a piece of middleware that authenticates a token that is being sent in a post request. Note that for the entire codebase I have everything stored in temporary storage i.e lists as it's only for testing. Below is the working JavaScript code:

app.get("/posts", authentificationToken, (req, res) => { if(posts.filter(post => post.username !== req.username)) res.json({ "message": "you have yet to upload posts"}) else { res.json(posts.filter(post => post.username === req.user.name)) } }) function authentificationToken(req, res, next) { const authHeader = req.headers["authorization"] const token = authHeader && authHeader.split(" ")[1] if(token == null) return res.status(401).send({error: "unauthorized"}) jwt.verify(token, ACCESS_TOKEN_SECRET, (err, user) => { if(err) return res.status(403).send({ error: "forbidden"}) req.user = user next() }) }

The issue arose once I migrated from JavaScript to TypeScript and also used a more organized codebase (Routers -> Controller -> Services). Where do I put my authentificationToken middleware in the structure shown below? As you can see I have catchErrors middleware already and have the problem adding authentificationToken to that middleware.

For context below is the order of this specific post request.

In my server file I have:

app.get("/posts", postsRoutes)

My route like this:

postsRoutes.post("/login", handleposts)

Tried adding authentificationToken before handleposts giving me No overload matches this call.

My controller with handleposts like this:

//normally authentifcationToken would be in a util but for convenience i have left it inside the controller export const authentificationToken = function(req: RequestCustomer, res: Response, next: NextFunction) { const authHeader = req.headers["authorization"] const token = authHeader && authHeader.split(" ")[1] if(token == null) return res.status(401).send({error: "unauthorized"}) jwt.verify(token, ACCESS_TOKEN_SECRET, (err, user) => { if(err) return res.status(403).send({ error: "forbidden"}) req.user = user next() }) } export const handleposts = catchErrors( async(req, res) => { // validate the request const request = postSchema.parse({ ...req.body, userAgent: req.headers["user-agent"] }) // call the service const posts = await getPosts(request) // return the response return res.status(CREATED).json(posts) } )

Tried doing authentificationToken(catchErrors(...)) but got Expected 3 arguments, but got 1. It seemed most right to add authentificationToken here but I don't know where.

The following is my getPosts:

export const getPosts = async(data: PostRequest) => { if(posts.filter(post => post.username !== data.username)) return ({ "message": "you have yet to upload posts"}) else { const post_result = posts.filter(post => post.username === data.username) return post_result } }

I have referred to How to make middleware respond on every request however using app.use(authentificationToken) in my server.ts gives me No overload matches this call.

I have also referred to Chaining multiple pieces of middleware for specific route in ExpressJS but I did not manage to translate this into my structure of how a request is being handled.

I have referred to the Express documentation but also here it adds the middleware in the standard app.get("/", middleware, function()).

Read Entire Article