authHandler
function authHandler(req: Object, res: Object, next: function)
async
Server side middleware to authenticate requests using a JWT token.
Extracts the Bearer token from the Authorization header, decodes it to get the user ID,
retrieves the user's secret, and verifies the token. If valid, attaches the decoded payload
to req.user
and calls next()
. Responds with 401 if authentication fails.
Returns: Promise<void>
PARAMETER | TYPE | OPTIONAL | DEFAULT | DESCRIPTION |
---|---|---|---|---|
req | Object | The request object. | ||
res | Object | The response object. | ||
next | function | The next middleware function. |
const { authHandler } = require("@pr4j3sh/auth")
app.get("/protected", authHandler, (req, res) => {
res.json({ user: req.user });
});