Cloudflare allows us frontend devs to build APIs in no-time. They will be edge deployed and don't need an extra API gateway! As I already wrote in one of my previous articles, it's built on web technology, you know from the browser: JavaScript and Service workers.
This is a blessing and a curse. On the one hand, we can use the APIs we know from the browser; on the other hand, some Node.js packages won't work here, because the APIs are missing.
That's why I wrote this how-to.
Pre-Requisites
You need to sign up for Cloudflare and Auth0. They both come with a generous free plan, so no fear!
You also need a Linux or macOS system with a Node.js installation.
The repo contains a credentials.json where you need to add your Auth0 and Cloudflare account details and a setup.js that will sprinkle your credentials into your the files that need them. You can look at the tmp.* files to check what credentials are required for your projects later.
The Cloudflare accountId can be found in the Cloudflare dashboard. You have to click on "Manage Workers" to get to it.
The Cloudflare email is the email address you signed up with to Cloudflare.
For the Auth0 credentials, you need to create an "Auth0 Application" first.
Creating an Auth0 Application
Create a "SPA Application" on the Auth0 Dashboard. There is a big orange button on the top right for that purpose.
The app name is not essential here. What is important is the "Domain" and "Client ID" that will be shown at the top of the settings after you created the app. These are the last two values needed for the credentials.json.
Next, update the application settings.
You have to add your Cloudflare workers.dev subdomain and URLs to the index.html so Auth0 knows which CORS headers it should use and where to redirect users after login.
Note: You have to run it with source ./setkeyenv.sh; otherwise, the variables will be gone when the next program is run in your shell.
Serving HTML with Cloudflare Workers
I didn't use Workers Sites here, the hosting product of Cloudflare. Instead, I integrated an HTML file as a string. This could also be done for other files like CSS, images, and JavaScript.
if (request.url.includes("/index.html"))returnnewResponse(html,{headers:{"Content-Type":"text/html"}});
Note: This only works if the string isn't too big, and you have to set the Content-Type header; otherwise, the browser won't recognize the HTML.
Getting a JWT from Auth0'S Lock Widget
The Lock widget would return something called an "opaque token" and not a JWT when I first run it. I had to supply it with the following config to get a JWT token. It will be stored inside authResult.idToken after an authentication happened.
Note: Auth0's /.well-known/jwks.json endpoint is rate-limited, and Cloudflare Workers scale pretty good, so you need to cache them in production. The best place would be Workers KV.
Summary
Cloudflare Workers are the most light-weight FaaS system I know and are even edge deployed by default. But they are also different from Node.js, which required me to jump through some hoops to get things running.
Auth0, on the other hand, is a high level managed serverless auth service that does much of the heavy lifting for you, so while finding the public keys wasn't that straight forward, it saves so much time, you can't quite imagine.
In the end, everything worked out, and I'm happy with the result.