I have used FaunaDB before, but only on the server in a small personal project. I had written a little analytics code, that recorded site visitors. I wanted to see if I could use that to replace a third party analytics library. Overall it worked well, though FQL took some effort to wrap my head around.
A recent talk by Brecht De Rooms and Eric Wyne made me realize just how truly easy and well thought out FaunaDB is. Here is the video from Next.js Conf 2020.
In the video Eric shows how a simple route on a server, can authenticate the user. This works because FaunaDB has built in authentication and roles. Once the server authenticates the user, it returns the users secret that allows the client to do all further actions, as that user. All the code below is from Eric’s MDX deck used during the presentation.
This is a sample
pages/api/login.jsfile. Where the
passwordis passed to the serverless function to return a client secret as simple text.
On the client the login route is called to get the clients secret. This is stored in the clients state for future queries. The thing that makes this elegant is the
useQuery()hook that takes a FaunaDB FQL query and executes it as the logged in client.
In the example above, the
useIdentity()hook can just execute the
q.Get(q.Identity())query without having to worry about the login aspects.
FaunaDB is truly built for the JAM Stack.
The simplicity here makes this highly maintainable. I was impressed with FaunaDB when I first used it almost 2 years ago. This presentation just enforces that, and proves that FaunaDB is truly built for the JAM Stack.