@iinfinity/jwt
v0.1.1
Published
JSON Web Token.
Downloads
6
Readme
JWT
JSON Web Token, see NPM & GitHub.
Change Log
0.0.0 => 0.1.0
- feat: JWT sign & verify
See CHANGELOG for detail.
Usage
See simple demo for more detail.
Install
npm i --save @iinfinity/jwt
Create
Create a default JWT instance.
const jwt = new JWT(); // same as `new JWT({ algorithm: 'HS512', key: String(Date.now()), urlsafe: false })`
Create a custom JWT instance with urlsafe:
const customJWT = new JWT({ algorithm: 'HS256', key: 'key', urlsafe: true });
Sign
Define header & payload first:
const header: Header = {
alg: 'HS256',
typ: 'JWT'
};
const payload: Payload = {
username: 'user',
password: 'pass'
};
Sign with payload & default header:
const token = jwt.sign(payload);
Sign with payload & header, you can set special algorithm in header:
const token256 = jwt.sign(payload, header);
Verify
Verify token & parse it:
const verified: JSONWebToken | undefined = jwt.verify(token); // if failed to verify, return undefined
const verified256: JSONWebToken | undefined = jwt.verify(token); // use header.alg as Algorithm
If failed to verify, it will return undefined.
Author
Devin Don(IInfinity), [email protected].