Last active
August 29, 2015 13:58
-
-
Save robwormald/9987338 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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'; | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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