App Updates
Since the last posting, I’ve gotten the itch to play with a different backend and tweak the navigation a bit. I was using Firebase (opens in a new window) services for a lot of the heavy lifting. Firestore (opens in a new window) for data storage/ACLs, Cloud Functions (opens in a new window) for “serverless (opens in a new window)” backend tasks, as well as their authentication (opens in a new window) service. I had dabbled a bit with their real-time database (opens in a new window) for app caching, and I had some Remote Config (opens in a new window). These all needed to be replicated if I wanted to migrate off and use something else.
Speaking of which, I wanted to shift to a self-hosted Postgres (opens in a new window) database. It can natively handle JSON blobs, which would come in handy when I needed to redesign the schema. It also has notifications and very flexible functions & stored procedures. I had heard Postgres (opens in a new window) can be your entire backend 😜, so that was my initial goal.
Firestore (opens in a new window) is a NoSQL (opens in a new window) document storage database. So I did not have tables in mind when first building the data model. However, it all translated into fairly normal tables, and my ACLs distilled to simple Row-Level (opens in a new window) policy rules. Thanks in part to PostgreSQL’s JSON column type! 😂
Postgres (opens in a new window) is cool and all, but it should not be publicly accessible, so I wanted to put it behind a gateway. Ideally, something that could generate an API or GraphQL schema from my database schema. I would have loved a GraphQL option, but I found Postgrest (opens in a new window) and Supabase’s postgrest.js (opens in a new window) and thought… that will do. 😂 Funny enough, my original Firestore (opens in a new window) queries and [Postgres.js] queries were structured similarly… not exactly swappable, but close enough that the code using them didn’t need to really change much.
I couldn’t find a way to use only Postgres (opens in a new window) to handle OAuth (opens in a new window) flows, and the idea of building my own (again1) didn’t seem palatable… So I played with a few off-the-shelf options. I started with Auth0 (opens in a new window)… mostly because I had heard of them and was curious about what they provided… However, after exploring their offerings, I felt like I would have traded one cloud service for another… and in the hour or two I spent exploring it, I did not find a convenient way to sync it with Postgrest (opens in a new window) so it could validate the JWT (opens in a new window).
Since my app is built on Expo (opens in a new window)’s framework, I looked to their documentation (opens in a new window) for more suggestions. Their first suggestion was Better Auth (opens in a new window). Now THIS is what I was looking for. Self-Hosted. Integrated. User definitions and data stay in my database. The only (minor) downside was that I needed to build my own service with it…
With auth/data sorted, I needed a way to get notifications from [PostgreSQL] back to the client, as well as a “node” app to host the Better Auth middleware (opens in a new window). I decided to use Bun (opens in a new window) to be my runtime (and package manager) for these two facets. Bun (opens in a new window) has a built-in web server API that handles websockets. The only dependencies I needed to add were a JWT library and a Postgres client. As for the auth bit, I only needed the same libraries as for the rest, plus Express (opens in a new window) and the Better Auth (opens in a new window) libraries. Another neat thing Bun can do is COMPILE everything needed to run your app into a single binary. Like bundling, but even better.
To keep things simple and separated by concern, I made each facet its own service, running in its own containers. And I tied them all together with nginx (opens in a new window) as the main reverse proxy (opens in a new window).
-
I’ve built many authentication forms/flows/apps/services over the course of my career. ↩