1

I have a route to view the information of a shop. The route looks like

router.param('userId',getUserById)
router.get("/store/:storeName/:userId?",isAuthenticated,getStoreDetail)

Based on the condition I want to send a response. In the route the userId is optional. If the userId is not provided it sends only particular information of the shop as a response. And if the userId is provided then I should authenticate the user and then send all the details of the shop.

So I created a middleware to achieve this.

const isAuthenticated = (req, res, next) => {
    const { user } = req
    if (user) {
        return [
            expressJwt(jwtTokenProperty),
            (err, req, res, next) => {
                if (err) {
                    return res.status(401).json({
                        status: false,
                        error: err.message,
                        isJWTError: true
                    })
                }
                const checker = req.user && req.auth && req.user.id == req.auth.id
                if (!checker) {
                    return res.status(401).json({
                        status: false,
                        error: "Invalid Request"
                    })
                }
                next()
            }
        ]
    } else {
        next()
    }
}

If the userId is not provided then I am getting a response as needed. But if the userId is provided I don't get any response.

1 Answer 1

2

You forgot to return next().

Edit: You need to return a response rather than array containing a function.

const isAuthenticated = (req, res, next) => {
    const { user } = req
    return user
        ? [expressJwt(jwtTokenProperty), processResponse(req, res, next)]
        : next()
}

const processResponse = (req, res, next) => {
    const checker = req.user && req.auth && req.user.id == req.auth.id
    if (!checker) {
        return res.status(401).json({
            status: false,
            error: "Invalid Request"
        })
    }
    return next()
}
Sign up to request clarification or add additional context in comments.

7 Comments

It's not working. My question is if there exists a user I should check whether he is a valid user and then send the response
Now that I look at it your code does seem curious. You're returning a middleware? I'm not sure a middleware (which is a function of three parameters) is valid to return.
I too had the same doubt. But I referred to this question - stackoverflow.com/questions/47572752/….
Oh but they're calling the middleware with arguments, not returning it! That's a difference! I've split it out and updated the example. :)
when we call processResponse() I am getting err is not defined. If I try to add err parameter to isAuthenticated then in the next middleware I am not able to access req parameter
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.