@allinmkt/jwt
v1.0.0
Published
Package to handle with all json web tokens provided by applications
Downloads
5
Keywords
Readme
JWT
Package to handle with all json web tokens provided by applications
Installation
$ npm install @allinmkt/jwt
Usage
In all cases, JWT needs to receive some paramters, are those:
| Name | Description | |----------|------------------------------- | | payload | Data to be converted in token | | token | Token that will be decoded | | secret | Secret to asign the token |
Generate a token
To generate a new token, you need to pass the payload and secret
import jwt from '@allinmkt/jwt'
function exampleGenerate() {
let payload = {
name = 'Jhon Doe'
age = 30
}
let token = jwt(payload, secret).generate()
return token
}
Decode an existent token
In case you need to decode a previous token, use this function
import jwt from '@allinmkt/jwt'
function exampleDecode(token) {
let decoded = jwt(token, secret).verify()
return decoded
}
Set token to header
For auth methods when you need to set the access token, use the header
function
import jwt from '@allinmkt/jwt'
function exampleHeader(token) {
jwt(payload, secret).header()
return
}
This function will return an object tha you had to set in your header manually
Response:
{
headers: {
x-access-token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoiSm9obiBEb2UifQ.DjwRE2jZhren2Wt37t5hlVru6Myq4AhpGLiiefF69u8'
}
}