Skip to content

Instantly share code, notes, and snippets.

@heytulsiprasad
Last active February 24, 2020 16:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heytulsiprasad/87cc908b96939b74d41cbd31ff49d566 to your computer and use it in GitHub Desktop.
Save heytulsiprasad/87cc908b96939b74d41cbd31ff49d566 to your computer and use it in GitHub Desktop.
gist overviewing methods under jsonwebtoken to generate and verify jwts
// we are going to explore jsonwebtoken methods
const jwt = require("jsonwebtoken")
const myFunction = async () => {
const token = jwt.sign({ _id: "abcdefg" }, "anysecretkey", {
expiresIn: "10 seconds"
})
console.log(token)
// eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiJhYmMxMjMiLCJpYXQiOjE1ODI1NjA4MzMsImV4cCI6MTU4MjU2MDgzOH0.KCjzcuHyg0VS-GVN8OnE_jqTh3LtixMGM3iqjUfeZoI
const data = jwt.verify(token, "anysecretkey")
console.log(data)
// { _id: 'abc123', iat: 1582560833, exp: 1582560838 }
}
myFunction();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment