How to Create and Validate JSON Web Tokens in Deno

How to Create and Validate JSON Web Tokens in Deno

In this Deno tutorial, we’ll see how to create and validate a JWT(JSON Web Token) in Deno. We’ll be using djwt, the absolute minimum library to make JSON Web Tokens in deno and Oak framework. What is JWT? JSON Web Token is an internet standard used to create tokens for an application. These tokens hold JSON data and are cryptographically signed.

In this post, we’ll see how to create and validate a JWT(JSON Web Token) in Deno. For this, we’ll be using djwt, the absolute minimum library to make JSON Web Tokens in deno and Oak framework

Before You Get Started

This tutorial assumes you have:

  • A basic understanding of JavaScript and Deno
  • Latest Deno version installed on your system

What is JWT?

JSON Web Token is an internet standard used to create tokens for an application. These tokens hold JSON data and are cryptographically signed.

Here is how a sample Json Web Token looks like

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6Im9sYXR1bmRlZ2FydWJhQGdtYWlsLmNvbSIsIm

JWT is a good way of securely sending information between parties. Because JWTs can be signed—for, you can be sure the senders are who they say they are. And, as the signature is generated using the header and the payload, you can also verify that the content hasn't been tampered with.

JWT can contain user information in the payload and also can be used in the session to authenticate the user.

If you want to know more about JSON Web Token, We have a very good article about it.

How to generate JWT token in Deno

First, let's set up a Deno server to accept requests, for it, we are using Oak framework, it is quite simple and few lines of codes as you can see below.

// index.ts
import { Application, Router } from "https://deno.land/x/oak/mod.ts";

const router = new Router();
router
  .get("/", (context) => {
    context.response.body = "JWT Example!";
  })

const app = new Application();
app.use(router.routes());
app.use(router.allowedMethods());

await app.listen({ port: 8000 });

Once our program is ready for accepting request lets import djwt functions to generate JWT token, In below code we can use a secret key, expiry time for JWT token in 1 hour from the time program will run and we are using HS256 algorithm.

Add the below code in index.ts and update the router as shown below, you can now get a brand new token on http://localhost:8000/generate

// index.ts
...

import { makeJwt, setExpiration, Jose, Payload } from "https://deno.land/x/djwt/create.ts";

const key = "secret-key";
const payload: Payload = {
  iss: "Jon Doe",
  exp: setExpiration(new Date().getTime() + 60000),
};
const header: Jose = {
  alg: "HS256",
  typ: "JWT",
};

const router = new Router();
router
  .get("/", (context) => {
    context.response.body = "JWT Example!";
  })
  .get("/generate", (context) => {
    context.response.body = makeJwt({ header, payload, key }) + "\n";
  })

...

deno jwt json security developer

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How long does it take to develop/build an app?

This article covers A-Z about the mobile and web app development process and answers your question on how long does it take to develop/build an app.

Learn to secure NodeJS APIs with JWT Tokens

Learn to secure NodeJS APIs with JWT tokens - Building secure NodeJS APIs with JWTs. What is a JWT? JSON Web Token (JWT) is an open standard, popularized by the good folks at Auth0.

What is JSON Web Tokens (JWT) and Why Should You use JWT

What is JWT and Why Should You use JWT? What makes JSON Web Tokens (JWT) secure? In this video I will explain in depth exactly what JWT is, how it works, why it is secure, and when you should use it. Let’s look at the implementation detail and usage of the popular methods of authorization: JWT(JSON web tokens) and see what makes it secure in the first place?

How to Apply JWT Authentication in Deno using Oak Framework

JWT Authentication in Deno REST API. I will show you how to apply JWT authentication in Deno using oak framework.

Offshore Software Development - Best Practices

To make the most out of the benefits of offshore software development, you should understand the crucial factors that affect offshore development.