It's been ~1 year since I switched to using Next.js instead of my usual React + Vite setup.
My partner has been transitioning to tech from medicine and I've been helping her with becoming a better software engineer.
She's currently working on her first web app! I thoroughly believe in the power of learning through doing and that's why we immediately jumped into building a project after she finished her html/css/js course.
As you might have guessed it, we're using the following stack:
React + JavaScript
Vite as our bundler
npm as the package manager
Supabase as our database and auth provider
TailwindCSS for styling
Shadcn/UI for components
React Router for routing
Vercel for deployment
So, without further ado, let's get into how I added Supabase Auth to her app.
Setting up Supabase
We had already done this step but make sure that you have a Supabase account and that you have created a project. Add your supabase credentials to the .env file.
Since we're using React Router for routing, I had to make a few changes to the code. The entry point of our app is src/main.jsx:
Similar to Next.js, the Layout component is used to wrap the entire app:
Well, this is pretty much it for the basic setup. However, we're note done yet.
Adding users to the database from the auth.users table
As you saw on the supabase doc that was linked above, when a user signs up, they are added to the auth.users table.
However, in most applications we want to store additional information about the user that is revelant to our app's logic (think profile pictures, subscription status, etc).
To do this, we need to use a supabase function that gets triggered when a user signs up. Follow the steps outlined below:
Make sure that you have a users table in your database.
Go to the supabase sql editor and run the following code:
This will create a function that inserts the user's email into the users table and then create a trigger that runs the function after a new user is created.
IMPORTANT: Make sure that the id column in the users table is a uuid type. Supabase uses int8 by default -- this will cause an error.
Now, test your changes by signing up through the auth UI.
How to make things better
If you've come this far your app should already be working. However, there are a few things that we can do to make things cleaner and more reusable.
At the moment, any component that requires access to user data needs to fetch that information from our DB. Let's fix this using React Context:
Create a new file src/contexts/UserContext.jsx and add the following code:
Now, wrap your main.jsx file in the UserProvider component:
Update the Layout component to use the useUser hook:
Now, any component that needs to access user data can use the useUser hook to get the user data:
Another nit that you might have noticed was that I'm importing the getDB function from ../lib/utils:
I just wanted things to be cleaner and more modular in case I needed to change the supabase client in the future.
Conclusion
That's pretty much it! I hope this helps you get started with Supabase Auth in your React Vite app.