CSP02 / ChatterBoxAPI

API for ChatterBox app
MIT License
1 stars 0 forks source link

Use Express middleware for JWT authentication to reduce code duplication #4

Open nt314p opened 1 month ago

nt314p commented 1 month ago

Many routes begin with the following set of code that verifies the JWT token or sends an error:

if (!req.headers["authorization"]) return res.status(401).send({ error: types.ErrorTypes.NULL_TOKEN })
    const [scheme, token] = req.headers.authorization.split(" ")
    try {
        const decoded = jwt.verify(token, process.env.JWT_SECRET, { complete: true })
...

By using some authentication middleware that runs before authentication-protected routes, the above authentication code does not have to be repeated.

CSP02 commented 1 month ago

oh I never know we can do something like that. Gonna implement it.