Authenticate requests to Cube with Auth0
Introduction
In this guide, you’ll learn how to integrate Auth0 authentication with a Cube deployment. If you already have a pre-existing application on Auth0 that you’d like to re-use, please skip ahead to Configure Cube. We’ll be creating an Auth0 application and API, configuring a rule on Auth0 to add custom claims to vended JWTs, and finally configuring Cube to use Auth0.Create an application
First, go to the Auth0 dashboard, and click on the Applications menu option on the left and then click the Create Application button.http://localhost:4000 for the Developer Playground).
Custom claims
You can also configure custom claims for your JWT token. Auth0 has two SDKs available; Auth0.js and the Auth0 SPA SDK. We recommend using the SPA SDK wherever possible, as per Auth0’s own developer advice. If you’re using@auth0/auth0-angular or @auth0/auth0-react, then the SPA SDK is
automatically included.
Open the Auth0 dashboard, click on ‘Rules’ and add a rule to add any custom
claims to the JWT.
Auth0 SPA SDK
Take note of the value of
namespace here, you will need it later to configure
Cube.Create an API
If you’re using the Auth0 SPA SDK, you’ll also need to create an API. First, go to the Auth0 dashboard and click on the APIs menu option from the left sidebar, then click the Create API button.cube-app), then click the Create button.
Take note of the Identifier here, as it is used to set the JWT Audience option
in Cube.
@auth0/auth-react package for your
application front-end, this might look something like this:
Configure Cube
Now we’re ready to configure Cube to use Auth0. Go to your Cube project and open the.env file and add the following, replacing the values wrapped in <>.
Testing with the Developer Playground
Retrieving a JWT
Go to the OpenID Playground from Auth0 to and click Configuration.- Auth0 domain:
<AUTH0-SUBDOMAIN>.auth0.com - OIDC Client ID: Retrieve from Auth0 Application settings page
- OIDC Client Secret: Retrieve from Auth0 Application settings page
- Audience: Retrieve from Auth0 API settings
access_token from the response, and use the JWT.IO
Debugger to decode the token and verify any custom claims
were successfully added.
Set JWT in Developer Playground
Now open the Developer Playground (athttp://localhost:4000) and on the Build
page, click Add Security Context.