Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
//issue a token, with an expiration date
var jwt = require('jsonwebtoken')
//issue token that expires after 10 mins AND has an issuer
var token = jwt.sign('helloworld','secret',{ expiresInMinutes : 10, issuer : 'hipster.io'});
//verify token, but don't check assertions.
jwt.verify(token,'secret',function(err,payload){
console.log(err) //-> undefined
console.log(payload) //-> 'helloworld'
})
//verify token is unexpired AND issued by hipster.io
jwt.verify(token,'secret',{ issuer : 'hipster.io'},function(err,payload){
console.log(err) //-> 'undefined'
console.log(payload) //-> 'helloworld';
})
//bad issuer fails...
jwt.verify(token,'secret',{ issuer : 'omfgdogs.io'},function(err,payload){
console.log(err) //-> 'bad issuer'
console.log(payload) //-> 'undefined';
})
var jwt = require('jsonwebtoken')
//simplest use case.
//issue token.
var token = jwt.sign('helloworld','secret')
//verify token
jwt.verify(token,'secret',function(err,payload){
console.log(err) //-> undefined
console.log(payload) //-> 'helloworld'
})
//bad secret
jwt.verify(token,'wrongsecret',function(err,payload){
console.log(err) //-> 'wrong secret' (or something...)
console.log(payload) //-> undefined;
})
//issue a token, with an expiration date
var jwt = require('jsonwebtoken')
//issue token that expires after 10 mins
var token = jwt.sign('helloworld','secret',{ expiresInMinutes : 10});
//verify token
jwt.verify(token,'secret',function(err,payload){
console.log(err) //-> undefined
console.log(payload) //-> 'helloworld'
})
//after 10 minutes
jwt.verify(token,'secret',function(err,payload){
console.log(err) //-> 'expired' (or something...)
console.log(payload) //-> undefined;
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment