DEV Community

badalkhatri
badalkhatri

Posted on

Deno + JWT

Alt Text

In this application, I am going to create simple JWT encode and decode functions.

First of all, we need to import two libraries or JWT from Deno's 3rd party packages

  • validateJwt — it will be used to validate our JWT token
  • makeJwt— it will convert our payload into JWT token
  • Jose — it indicates algorithm and type
  • Payload— it is interface for our Payload or Data

Encode

  • First of all we will put our data which into payload by using Payload interface (it is mandatory).
  • Then we will define our header which will contains algorithm and type.
  • Now we will pass header, payload and key (for simplicity I have put this key in same file but it should be placed in Environment Variable)

This function will return Promise that will contains our JWT token, we can also put expiration time in our payload.

Decode

In this function we will simply take the JWT string and pass it into validateJwt function with the key.

We will extract our payload from the return data.

Using this functions I have made simple REST API /encode and /decode.

You can checkout the code for more from my repository here

Happy coding :)

Thank you

Top comments (0)