jwt token parser npm code example
Example 1: jwt token example in node js
function authenticateToken(req, res, next) {
// Gather the jwt access token from the request header
const authHeader = req.headers['authorization']
const token = authHeader && authHeader.split(' ')[1]
if (token == null) return res.sendStatus(401) // if there isn't any token
jwt.verify(token, process.env.ACCESS_TOKEN_SECRET as string, (err: any, user: any) => {
console.log(err)
if (err) return res.sendStatus(403)
req.user = user
next() // pass the execution off to whatever request the client intended
})
}
Example 2: npm jwt decode
import jwt_decode from "jwt-decode"; var token = "eyJ0eXAiO.../// jwt token";var decoded = jwt_decode(token); console.log(decoded); /* prints: * { foo: "bar", * exp: 1393286893, * iat: 1393268893 } */ // decode header by passing in options (useful for when you need `kid` to verify a JWT):var decodedHeader = jwt_decode(token, { header: true });console.log(decodedHeader); /* prints: * { typ: "JWT", * alg: "HS256" } */