Getting started with the new Auth0 extension on Netlify
by
Domitrius Clark
In collaboration with Auth0, we’ve released a new extension to connect your Auth0 tenants to your Netlify account dashboard and add authentication to your Netlify site!
If you already have a site and just want to enable the extension, you can skip to the next section.
For educational purposes, we’re going to start with a brand new website on our Netlify account. We’ll be using Astro to build our app. Run the following command and select the default values:
Terminal window
npmcreateastro@latest
Once our project is created, we’ll need to make a couple changes to the Layout.astro and index.astro files:
Next, we’ll install the Auth0 JS SDK to streamline login, logout, and help us generate a token to protect our serverless functions.
Terminal window
npminstall@auth0/auth0-spa-js
This page, while not authenticated, will show a header and a log in button. Eventually, when authenticated, we’ll be able to see a logout button and some of the logged in user’s details. (We append these later in index.astro within a script tag to the profile-data container.)
Now that we’ve completed setting our site up and connecting to Netlify we can move to installing and enabling the extension:
In the team Dashboard, navigate to the Extensions page from the sidebar.
Search for Auth0 and select it from the results, then install from the details page.
From the team’s Sites list, select the site you plan to use with this extension, navigate to the Access & security settings page and scroll to find the Auth0 extension settings.
Next, start the process to link your Auth0 tenant to our Netlify account. Select Link an Auth0 tenant and follow the prompts to authorize Netlify to access your tenant.
Once you’ve connected your tenant, your next step is to configure the extension for your site.
Under Site tenants, select Add a tenant and select a tenant to use for your site’s deploy contexts.
Select the Auth0 application and, optionally, an API. If you don’t already have Auth0 application and APIs, we recommend that you create them from the Netlify UI. Creating Auth0 applications and APIs from the Netlify UI optimizes their settings for use with Netlify.
Select one or more deploy contexts for your site tenant.
Branch deploy limitations
This extension does not support assigning a tenant to individual branch deploys. The tenant selected for the Branch Deploys context applies to all branch deploys.
This extension automatically creates a set of environment variables based on your configuration. If your framework or build system requires the use of a specific prefix in environment variable names, input your desired prefix or select one from the Preset dropdown. If your framework or build system doesn’t require a prefix, leave this blank.
Environment Variables
For our project, we’re going to need to add prefixes for Astro to recognize a few of our environment variables in the client. Add PUBLIC as the prefix. This will prefix all of your client based environment variables with PUBLIC.
You can review these variables at any time by navigating to Site configuration > Environment variables.
To configure multiple tenants, repeat these steps for each tenant.
With the extension now enabled, it’s time to dig into some code and set our application up to utilize Auth0 to set up our authorization flow alongside protected Netlify functions.
Over the next section, we’ll:
Build utility functions to:
handle client authentication actions like login, log out, and token generation to pass to our Netlify functions
handle server side authentication actions like reading & verifying the token from the request inside of our functions
Build a serverless function that will verify your token and fail or succeed based on that verification
Connect to Auth0’s SDK and send the user to an Auth0 hosted Sign in/up page
First, we’ll create a couple of files to hold the functions we’ll be using across the client and server. Inside of your base directory, create a utils folder with two files:
Terminal window
mkdirutils
touchutils/server-auth.tsutils/client-auth.ts
Client vs Server Code
To make sure we don’t bundle our server code into our client bundle, we make this distinction between client and server utilities.
Our server function will be used to verify the token from the request inside of our functions. First, we’ll need to install the jose package for JWT verification:
Terminal window
npminstalljose
npminstall@types/node-D
Why jose?
jose is a JavaScript module that implements JSON Web Token (JWT) and JSON Web Signature (JWS) standards. It’s particularly well-suited for token verification in Node.js environments.
if (location.search.includes("code=") &&location.search.includes("state=")) {
awaithandleCallback();
attachEventListeners();
awaitupdateUI();
} else {
attachEventListeners();
awaitupdateUI();
}
</script>
Local Development
During the process of enabling the extension, Netlify will set the site URL for the Auth0 Application URIs. If you’re developing locally, don’t forget to set your URL for your local server in your Auth0 application settings.