Supabase
Nuxtbe utilizes Supabase for authentication, data storage and edge functions. The Kit ships with all the relevant migration files, edge functions and webhooks. Here you can find the instructions on how to configure Supabase for your project.
Supabase project setup
First step is to create a Supabase account and project. In order to do this, you need to follow these steps:
- Create a Supabase account.
- Create a new project in the Supabase Dashboard.
- Enter your project details.
- Wait for the new database to launch.
Installing the Supabase CLI
Before we get started we need to install the Supabase CLI. You can find the details in the Supabase CLI documentation.
Supabase Database setup
Before running the migration, you need to link your local project to a remote database by running supabase link --project-ref <project-id>
. You may need to run supabase login
to login to your Supabase account if you didn't do it yet.
Project ID can be found on the API Settings page in the Supabase Dashboard.
Under the supabase/migrations
directory, you can find the SQL file which is used to create the database tables. All you need to do is to run the following command to apply the migrations:
This will create the tables needed for authentication and other features as well as functions and triggers needed for the application to work.
Supabase Auth setup
Supabase supports Email/Password authentication by default. But in order to use Oauth authentication or Magic Link authentication, you need to do some configuration in the Supabase Dashboard.
Please read the Supabase Auth documentation on how to set up third-party providers.
After you have set up the providers, you can use oauth authentication using example method in the pages/sign-in.vue
file called signInWithOAuth
.
Supabase Edge Functions/Hooks setup
Nuxtbe comes with a set of edge functions that are used to handle authentication and post-login emails. You can find them in the supabase/functions
directory. You can deploy them by running the following command:
Keep in mind that you have to have Docker daemon up and running to deploy the functions.
send-auth-mail-hook
function should be deployed without the jwt verification enabled. To do that you may run the following command:
After you have deployed all the functions, you can find them in the Supabase Dashboard under the Edge Functions tab.
Next step is to configure the webhook for sending authentication emails. To do this, you need to go to the Hooks tab in the Supabase Dashboard and create a new Send Email hook
. Hook type should be HTTPS
, and url should look like this:
This url will point to the send-auth-mail-hook
function which sends custom emails to the user during the authentication process.
Then generate the webhook secret and add it to the Edge Function Secrets
page.
You need to add the hook secret SEND_EMAIL_HOOK_SECRET
and Resend API key RESEND_API_KEY
to the Edge Function Secrets Management page.
You can get the Resend API key from the Resend Dashboard. Details on how to set up Resend can be found in the Emails section.
Also you need to add the BASE_URL
variable to the Project Variables page.
Send Email hook secret should be added to the Edge Function Secrets
page without the v1,whsec_
prefix.
Next, let's configure automatic user profile creation on first login. Follow these steps:
- Navigate to Database Webhooks in the Supabase Dashboard
- Create a new webhook with the following settings:
- Table:
users
- Trigger:
insert
- Type of webhook:
Supabase Edge Function
- Function:
user-created-hook
- Table:
- Add HTTP Header by:
- Clicking "Add a new header" in "HTTP Headers" section.
- Selecting
Add auth header with service key
from the dropdown. Dropdown will be visible if you've chosenSupabase Edge Function
webhook type. - This will automatically add the header
Authorization: Bearer <your-service-key>
- Click on the "Create webhook" button to save the changes.
Supabase custom SMTP setup
To use custom SMTP for sending emails, you need to add SMTP credentials to the Project Variables page.
For production use, it's highly probable that you will want to change rate limits for the SMTP service. It can be found here: SMTP Rate Limits.
Project variables configuration
To link your Supabase project to the application, you need to configure the supabase
credentials in the .env
file located in the root of the project.
Those keys can be found on the API Settings page in the Supabase Dashboard.
The SUPABASE_SERVICE_ROLE_KEY
should be treated as a highly sensitive credential - it must only be used server-side and never exposed to the client browser or end users. Keep this key secure and never include it in client-side code or public repositories.
Supabase module configuration
You can configure the Supabase module in the nuxt.config.ts
file. By default, the module is configured to redirect users to the /sign-in
page when they are not authenticated. You can customize this behavior by modifying the redirectOptions
property. To exclude certain routes from authentication, you can use the exclude
property.