Supabase
This page will show you how to integrate Auth with the Supabase BaaS solution to let your users securely link their wallets to their Supabase accounts.
Supabase doesn't yet support custom authentication methods, so you can't yet have users login with just their wallets - but you can link wallets to preexisting Supabase auth accounts, allowing you to interact with your users onchain.
If you want to interact with the code for the Auth + Supabase integration that we'll be building in this guide, you can checkout the following GitHub repository, or clone it with the command below:
npx thirdweb create app --template thirdweb-auth-supabase
Prerequisites
Before starting with integration, you'll need to create a new Supabase project. You can do this by heading over to the Supabase Dashboard, creating an account if you don't have one, and then clicking the button to create a new project.
Next, we'll need to set up an authentication provider on Supabase for our users to login. These will be the accounts that we link our user's wallet addresses to. In this guide, we're going to use the Google Authentication provider, which you can learn to set with the Supabase Google Auth documentation - but you're free to use any of the other authentication providers supported by Supabase.
Note that to set up Google Authentication, you'll need to create a new project with OAuth enabled on the Google Cloud Console, and then add the Google OAuth credentials to your Supabase project.
Setup
Once you've created your Supabase app and set up Google authentication, you'll be ready to start integrating Auth into your application.
To begin with, let's create a new Next.js project with thirdweb configured:
From within the created project, we'll need to install the @thirdweb-dev/auth
and @supabase/supabase-js
packages:
Configure Supabase
Next, you'll need to configure your application to communicate with the Supabase project you set up.
To do this, create a .env.local
file in the root of your project, and add the following environment variables, which you can find on the settings page of your Supabase project:
Create a new directory called lib
and create two helper scripts to initialize Supabase in the browser and server:
Now we have an easy way to access Supabase Auth and Database in both client and server environments!
Configure thirdweb Auth
Finally, to configure thirdweb Auth, we just need to add the NEXT_PUBLIC_THIRDWEB_AUTH_DOMAIN
environment variable to the .env.local
file as follows:
NEXT_PUBLIC_THIRDWEB_AUTH_DOMAIN=<thirdweb-auth-domain>
The NEXT_PUBLIC_THIRDWEB_AUTH_DOMAIN
is used to prevent phishing attacks - and is usually set to the domain of your project like example.com
. You can read more about it in the thirdweb Auth Documentation.
ThirdwebProvider
Inside the pages/_app.tsx
file, configure the authConfig
option with your domain:
Sign in users
Now that we've set up the provider, we can set up the initial flow to allow users to sign in using Supabase authentication. In this case, we'll use the supabase.auth.signInWithOAuth
function with the Google provider, since we configured that provider on the Supabase dashboard.
Getting the user data on the client side
Once users log in, we'll want a way to identify that they have actually successfully logged in. To do this, we'll use the supabase.auth.getUser
and supabase.auth.getSession
functions to keep track of the logged-in user whenever the user's authentication state changes.
We can bundle this functionality into a convenient hook as follows:
Create an API to link wallets to accounts
After the user's login, we want to create a way for them to associate their verified wallet address to their Supabase account. We can do this by creating a new API endpoint that verifies the client-side user's wallet address, and then updates their account on Supabase.
We can accomplish this with the following code:
Here, we use the verifyLogin
function to ensure that the user has sent a valid, signed login payload to the backend, which we can use to securely extract the wallet address of the client-side user. Once we do this, we can update the user database entry by getting the user id from the client-side access_token
, and then using the supabase.auth.admin.updateUserById
function to update the user's metadata with their wallet address.
Linking wallet addresses to accounts
Finally, with everything set up, we're ready to actually implement the client-side flow of prompting the user to sign a sign-in with a wallet message, sending it to the backend, and then linking the user's wallet address to their Supabase account.
Here, we use the useAuth
hook to call the thirdwebAuth.login
function and prompt the user to sign a login message. Then, we post this payload, along with the user's current session access token to the /api/link
endpoint we created to finish linking the user's wallet address.
Importantly, we now have access to the user's address via the user.user_metadata.address
field, which we can use to make queries or policies in our database!
So with these steps, we've now set up our Supabase to allow users to sign in with Supabase authentication and then link their wallets to their accounts!
You can now unlock all the functionality of Supabase Auth and Supabase using wallets!