This article provides a guide for setting up a Node.js API with Auth0, including creating a new API in the Auth0 admin, building an API that is secured using Auth0, and managing user access with roles and permissions.
Abstract
The article begins by introducing Auth0 and its benefits for identity management in new applications. It then guides the reader through the process of creating a new API in the Auth0 admin, setting up a Node.js API, and securing it with Auth0. The article also covers how to manage user access with roles and permissions, including creating permissions and validating them in the API. The article concludes with a note on the benefits of using Auth0 for production applications.
Bullet points
Auth0 is a powerful tool for identity management in new applications.
To create a new API in Auth0, you need to enter a name and identifier.
Auth0 will automatically create a test application for testing your new API.
To set up a Node.js API, you need to create a new Node.js project and install dependencies through npm.
The article provides code for creating a server and securing it with Auth0.
The article covers how to validate permissions in the API using a middleware function.
To check the permissions in a JWT, you can paste it into JWT.io.
Auth0 is a powerful tool for identity management and can be used for production applications.
If you have read my previous articles on Auth0 then you know I am a big fan of using it for identity management in new applications. In this article I will be demonstrating how to create an API in the Auth0 admin, building an API that is secured using Auth0, and finally will be walking through how to manage user access with roles and permissions.
To complete the demonstration API in this article you will need to have an Auth0 account already set up or create a new one. If you need some guidance on how to do that be sure to check out my previous Auth0 article which I will link below.
Previously I have written an article that is an introduction to Auth0 and provides as a guide for setting up a new React app and securing it with Auth0. Be sure to check out that article below.
Before we can start building our Node.js API we will need to set up a new API in the Auth0 admin. After you create or sign in with your existing account you will want to navigate to the APIs section in your admin UI.
Screenshot by the author
To create a new API you will be presented with a dialog and need to enter a Name and Identifier. Auth0 recommends the Identifier be a URL. If you know what domain you would be deploying the API to you could that as an identifier. I will just be using a logical name for the demonstration.
Screenshot by the author
After you have successfully created the new API you will be taken to the Settings view for that API. You will want to note these settings for when we get to setting up our API here a bit later on.
Screenshot by the author
Auth0 will automatically create a test application for testing your new API. You can see in the screenshot below that you can see this test application by going to the “Machine to Machine Applications” tab.
Later in the article, you will need the client_id and client_secret to generate access tokens for testing out our Node.js API. You can find those setting by clicking on the link to that newly created test application.
Screenshot by the author
The test application is an example of a Machine to Machine application which in other words would be one of your services calling the API with a bearer token. This is what we will be using for the demonstration in this article but note you can also use the bearer tokens generated in your Auth0 Single Page Applications.
That takes care of the initial admin section of this guide. Next, we will be moving into setting up the Node.js API.
The API
We are almost ready to begin writing code for our demonstration API. But before we can jump into the code we will need to create a new Node.js project and install a few dependencies through npm.
Create a new directory and enter the following commands in a terminal within that directory.
This step is optional, but if you want the API to automatically restart after you make file changes then I recommend adding nodemon to the dependencies.
npm install --save-dev nodemon
Our dependencies are all installed and now we can create index.js at the root of the project where we will create the server. The code to start is below for the server file.
Nothing much to the server as you can see. You will just need to replace audience, issuer, and jwksUri with the values from your own API. We are usingjwt middleware to validate the bearer tokens sent in the Authorization header. Note that because we have been setting this middleware at the base of the express app it will apply to every endpoint we create.
We could selectively apply this to some endpoints and leave others unsecured, but if we use it as above it will apply to every route we create.
Next, we are ready to run the API with either command below in the terminal.
npx nodemon index.js
// or
node index.js
Now with the API running, we will use curl to generate an access token with the test application created earlier. Use the following command to generate a token. Make sure to replace the example values with your own where necessary.
You will want to copy and paste the entire value of access_token that is sent. We will be adding this JSON Web Token (JWT) to the Authorization header when we call the API.
Just for a quick experiment to make sure our endpoint is secured we will attempt a call to the API without it. Use the following curl command in a terminal to test.
curl --request GET \
--url http://localhost:8000/secured
The result should look something like the following:
The result comes back as a chunk of HTML, but you can see the basic idea is that is this is UnauthorizedError that is shown if we try to call the secured endpoint without an authorization token. So that’s good, exactly what we would expect.
Next, we will try once again but this time with the authorization token. Note you will replace the token after “Bearer” in the authorization header.
This time you should get back our success message: {“success”:true}
Validating Permissions
Now that we have done the basics let’s move on to authorization or validating the access token to see if it contains the proper permissions for what it is trying to access on the API.
So the first thing is we will need to jump back into the Auth0 admin to create a few permissions. Follow the screenshot below and create these in your API:
create:users
read:users
delete:users
Now that we have defined the permissions for our API we will want to add a few more endpoints to make use of these. Add the following code to the existing index.js file.
We create a function for checking permissions that we use as a middleware function for our new endpoints. We can pass checkScopes multiple permissions to require or just single permission as we have done above.
If we run the API with the latest updates and update the URL from the previous curl command to point to GET /users we receive the following message: Insufficient scope
This is exactly what we expect so our new permissions validation is working as expected. To get this to work we can simply add these permissions to the testing application we are using to validate the API.
Screenshot by the author
We want to check one or all of the permissions in the APIs tab under the testing application. Once this is complete we will need to create a new token and be sure it contains the expected permissions.
There is an easy way to check this. Copy the JWT and paste it into JWT.io. This will decode the token and you can easily inspect it as a JSON object.
Screenshot by the author
If we check in the payload section in the scope property we can see the token contains all the permissions we just added so if we call the API again with the new token it should be successful this time.
I hope you have found this guide to getting started with Node.js and Auth0 helpful. Auth0 is a powerful tool for identity management and I have had a great experience using for production applications and like to help others do the same. Thanks for reading!