shJWT is a simple library designed to offer JSON Web Token (JWT) encoding and decoding functions, using HMAC with SHA256 for signing.
Install via npm:
npm install --save shjwt
Use the library by requiring it and calling the encode
and decode
functions.
const shjwt = require('shjwt');
const secret = 'the_secret_key';
const payload = {
'user_id': 1,
'email': 'test@example.com'
};
// Encoding
shjwt.encode(payload, secret)
.then(jwt => console.log(jwt)) // prints the encoded JWT
.catch(err => console.error(err));
// Decoding
const jwt = 'your.jwt.token.here';
shjwt.decode(jwt, secret)
.then(decodedPayload => console.log(decodedPayload)) // prints the decoded payload
.catch(err => console.error(err));
Converts the provided payload into a JWT.
payload
(Object): The payload that will be encoded.secret
(string): The secret key for generating the HMAC.- Returns a Promise that resolves with the JWT token.
Decodes the JWT into the original payload.
jwt
(string): The JWT to be decoded.secret
(string): The secret key for verifying the HMAC.- Returns a Promise that resolves with the decoded payload.
To test, ensure you have installed the necessary development dependencies:
npm install --dev
Run tests with:
npm test
Please use this library responsibly and ensure that you understand security best practices for JWT usage.
If you'd like to contribute, please fork the repository and use a feature branch. All contributions are welcome and greatly appreciated.
This project is licensed under the GNU GPLv3 License - see the license file for details.