Announcing React-Netlify-Identity-Gotrue!

Jon Sully

4 Minutes

Bringing ALL of the Netlify Identity features to React / Gatsby / Next

@japhethmast - Unsplash

It’s Done!

After an extensive amount of research and effort, I’m very excited to release a comprehensive Netlify Identity integration for React apps (aimed squarely at Gatsby and Next.js but any React app will work). Written purely in React and following React Hooks methodologies without any dependencies, this thing kicks butt! I’m a bit biased.

This article intends to give a bit of background as to the ‘why’ this package was created, but for starters, here’s a bit of an overview of what’s just been released:

Starting from the bottom, react-netlify-identity-gotrue is the core of it all - it’s what provides the interface between a React app and Netlify’s Identity service with easy-to-use, well-documented bindings. It handles end-to-end auth workflows from invites, sign-ups, email confirmations, forgotten passwords, Authenticated Functions (e.g. Auth + Netlify Functions), flexible user metadata, and plenty more. It does all of that purely leveraging React Hooks and fetch - zero dependencies and powered by direct HTTP calls to your site’s Netlify Identity service.

Sitting above that, gatsby-plugin-netlify-identity-gotrue is a Gatsby plugin that automatically configures the NetlifyIdentityContext to wrap the Gatsby React tree root (so that identity is available everywhere in the site).

And consuming that plugin is the demo site - https://gatsby-identity-demo.jonsully.net / gatsby-plugin-netlify-identity-gotrue-demo.

Together, this stack provides both flexible layering and self-evident documentation into how to use the core react-netlify-identity-gotrue library. In addition, each of these repositories is well-documented and the react-netlify-identity-gotrue library contains an exhaustive description of each of the methods and values it exports. Give it a spin!

Alright but why?

So from a historical standpoint, there were two primarily available ‘stacks’ for consuming Netlify Identity prior to this package-set being released. The first was authored by Netlify themselves and consists of the following layers:

And the second was authored by @sw-yx back when he worked with Netlify and involves a couple extra layers, but this stack is the current major means of integrating Netlify Identity into React. The layers are as follows:

So if two perfectly-fine integrations exist to implement Netlify Identity, why bother making a third? The answer lies in gotrue-js. The short story is that gotrue-js is a ‘vanilla-js’ library - it’s not connected to or aware of the React runtime at all. This can present issues when attempting to interface or interop with React. State can fall out of sync slightly, certain processes can’t get run exactly correctly, and generally things tend to be a little dirty. In the case of the former stack above, there are no React bindings at all. In order to interface with React you’d need to interact with events passed directly from window.

Additionally, the latter stack above doesn’t fully implement the needs of an Identity-enabled site. Password resets, email address changes, invitation tokens, etc. All of these things are critical for a production-ready app but aren’t supported by the latter stack above.

Putting these issues together, it felt like there was an opportunity to create a fully-featured pure-React integration for Netlify Identity that takes away the pain of cross-library interops and gives Application devs the ability to use the full Netlify Identity pipeline. react-netlify-identity-gotrue was built out of countless hours of digesting gotrue-js and gotrue code to re-engineer GoTrue’s HTTP API in React with a number of asynchronous hooks and state controls. It also takes into account so many hours spent with various members of The Community (feel free to find me there!) determining what feels hard, what feels broken, and what feels.. ergonomically bad. This library attempts to fix a number of those things and provide React developers with a single, unified interface for fully integrating Netlify Identity into their projects.

How do I get started?

Well for starters, go play around with the demo site! See if you can break it or if you can hack it. Then read the code underneath it and get an understanding of how the identity API works. You can follow that up by giving the react-netlify-identity-gotrue README a solid read-through to get a more fine-grain sense for what-goes-where, then finally you can build your own Gatsby, Next.js, or Create-React-App site that integrates react-netlify-identity-gotrue (or gatsby-plugin-netlify-identity-gotrue for Gatsby)!

Hope you enjoy!

-

Jon

Comments? Thoughts?

Colinrim

thanks so much for your time an effort to re-do an all-inclusive, clean solution from scratch ;) however, i played around a bit and things seem epic, except the pw-reset. this is what the console says after clicking the reset-email-link. Recovering User Refresh goTrueToken in 3359 seconds Logging Out Recovering User POST https://myapp.netlify.app/.netlify/identity/verify 404 Recovery token attempted that's already been consumed; logging out The example <AuthOverlay /> is used within Layout component and the pw-reset ‘popup’ shows for a split of a second (until logout, for what reason?).

thanks for any hints what i might be missing. cheers

Jon Sully (Author)

Hey there! Thanks for the heads up. I’ll dig into this and check it out! Do you have an example repo that you encountered this with?

Colinrim

its for the work-in-progress gatsby-site; sensatia.netlify.app. if it helps i can invite you to the repo as its not ‘public’ yet :)

Jon Sully (Author)

Sure thing, if you want to invite me to the repository that’ll definitely help me get a closer look at debugging. I am seeing the strange password reset behavior on your site, although I’m not yet sure why that’s happening. I can’t reproduce on https://gatsby-identity-demo.jonsully.net so I’ll definitely be curious to dig in more and squash a bug if there is one!

Colinrim

invitation sent. if you want we can talk it thru in slack if that simplifies anything :)

Anonymous

Hi there thanks for the work you put together! I just wanted to know if hitting the functions requires an upgraded plan of netlify identity. I copied and pasted the code and the build succeed but I’m getting 405 response when trying to sign-up, not too sure what I’m missing, haven’t done… thanks for your help anyway

Anonymous

Hey there! Jon here 👋🏻 Apologies for the anonymous response. Commento (my underlying comment service provider) appears to be having some login issues.

Nope, you shouldn’t need any paid features from Netlify to make everything work. Everything appears to be working with the demo app too and it’s not using any non-free features 🤔

Reply

Please note: spam comments happen a lot. All submitted comments are run through OpenAI to detect and block spam.