@iinfinity/jwt
TypeScript icon, indicating that this package has built-in type declarations

0.1.1 • Public • Published

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), I.INF@Outlook.com.

LICENSE

THE MIT LICENSE.

Readme

Keywords

Package Sidebar

Install

npm i @iinfinity/jwt

Weekly Downloads

1

Version

0.1.1

License

MIT

Unpacked Size

7.62 kB

Total Files

11

Last publish

Collaborators

  • devindon