Last active
March 13, 2017 09:34
-
-
Save mesaugat/96216596b54bc0631e31ff9909640a7e to your computer and use it in GitHub Desktop.
Generate a JWT
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
/** | |
* npm install jsonwebtoken | |
* | |
* node --harmony jwt.js | |
*/ | |
const jsonwebtoken = require('jsonwebtoken'); | |
const expiresIn = '1m'; | |
const object = { name: 'WTF' }; | |
const salt = 'WhatTheActualFuck'; | |
const jwt = { | |
/** | |
* Generate a JWT. | |
* | |
* @param {object} data | |
* @returns {string} | |
*/ | |
generateToken(data) { | |
return jsonwebtoken.sign({encryptedData: data}, salt, {expiresIn}); | |
}, | |
/** | |
* Verifies and decodes a JWT. | |
* | |
* @param {string} token | |
* @returns {object} | |
*/ | |
decrypt(token) { | |
return jsonwebtoken.verify(token, salt); | |
} | |
}; | |
const token = jwt.generateToken(object); | |
const jwtObject = jwt.decrypt(token); | |
console.log('\n', token); | |
console.log('\n', jwtObject); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment