Skip to main content

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>

PARAMETERTYPEOPTIONALDEFAULTDESCRIPTION
reqObjectThe request object.
resObjectThe response object.
nextfunctionThe next middleware function.
const { authHandler } = require("@pr4j3sh/auth")

app.get("/protected", authHandler, (req, res) => {
res.json({ user: req.user });
});