jwt token 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: what is jsonwebtoken
JSON Web Token is an Internet standard for creating data with optional
signature and/or optional encryption whose payload holds JSON that asserts
some number of claims.
The tokens are signed either using a private secret or a public/private key.
Example 3: jsonwebtoken
jwt.sign({ exp: Math.floor(Date.now() / 1000) + (60 * 60), data: 'foobar'}, 'secret');