Token Authentication

You will learn how to make a simple token authentication in Deno

Let's move on to creating a simple token authentication server. We will use the same code as in the previous step, but we will be changing things up a tad. Start by copying the code from the previous step into a new folder.

Add a hash set as a const to keep track of the tokens.

We want to be able to send a token in the URL like so:

But before we can do this, we need to receive a token, store it in the hash set for later use, and return it from the server. To do this, we send a response back in the body of the request. The logic underneath does not handle any of the input from the request, but simply generates a hash, stores it in the set, and returns the hash to the requester as plain text.

Try to visit the address in your browser! You should receive a token that is also stored on the server.

The next step is to add some logic for when you already have a token. Change the server to include this.

The flow is now checking the request URL for a hash. To illustrate this, we can look at the snippet below.


This page is a preview of Build and deploy a REST API with Deno

Start a new discussion. All notification go to the author.