1. 12
    Automatically Create a Stripe Customer for Each User with Supabase Function Hooks
    3m 50s

Automatically Create a Stripe Customer for Each User with Supabase Function Hooks

InstructorJon Meyers

Share this video with your friends

Send Tweet

We want to call our API route to create a new Stripe customer anytime a new user signs into our application. Since this triggers an insert on our profile table, we can use hooks in Supabase to listen to insert events and call our API route.

Function hooks in Supabase are similar to PostgreSQL triggers, however, rather than executing a function, we can call a HTTP endpoint. Since our Next.js application is running locally on our machine it is not possible for Supabase to send a HTTP request directly to our API route.

Ngrok is a simple tool that allows us to tunnel traffic from a publicly accessible URL on the internet, through to our Next.js application running locally.

In order for our API route to process the request, we need to tell our Supabase hook to also send our API_ROUTE_SECRET. Query parameters can be declared as HTTP params for our hook.

Lastly, we want our Stripe customer to be associated with our user via their email address.

~ 3 years ago

Hello Jon! I stuck in the function hook, when I try to login after writing the function returns this "Failed to invite user: Database error saving new user"... is there anyone can help ?

Jon Meyersinstructor
~ 3 years ago

Hmmm. Very strange! One thing you can check is the logs in your Supabase dashboard. It is under Settings > Logs > Database. If that doesn't give you any more hints, email support@supabase.io and include your project ref (the part of your Supabase URL after /project/), and hopefully someone will be able to take a look πŸ‘

George
~ 2 years ago

When I remove row level security in Profile table, stripe_customer updates. But with it on, it doesn't. I added the 'read' row level security from prior lesson. Has something changed?

Lucas Minter
~ 2 years ago

When I remove row level security in Profile table, stripe_customer updates. But with it on, it doesn't. I added the 'read' row level security from prior lesson. Has something changed?

Here is a link to the row-level security article on Supabase. Jon also has an in-depth video in that article that should answer your question. https://supabase.com/docs/guides/auth/row-level-security

James
~ 2 years ago

"Hello Jon! I stuck in the function hook, when I try to login after writing the function returns this "Failed to invite user: Database error saving new user"... is there anyone can help ?"

I'm running into this as well. I followed the steps for creating the function hook and when I try logging in, I get an error:

http://localhost:3000/?error=server_error&error_description=Database+error+saving+new+user

James
~ 2 years ago

I ran across this, and it solved part of my problem. I'm able to login but I'm still seeing errors. Not sure if it has to do with my local environment or not: https://github.com/supabase/supabase/issues/4883

James
~ 2 years ago

When I remove row level security in Profile table, stripe_customer updates. But with it on, it doesn't. I added the 'read' row level security from prior lesson. Has something changed?

@George did you ever get that working?

Jon Meyersinstructor
~ 2 years ago

Hey all πŸ‘‹ At this point, RLS should not be enabled on the profiles table. Once this is enabled, we need to use the service role key when creating a Supabase client. This will bypass RLS, which is what we want because this is a server action, not based on a specific user.

Jump forward to this lesson for creating a service role Supabase client, and fixing this API route πŸ‘

https://egghead.io/lessons/supabase-use-the-supabase-service-key-to-bypass-row-level-security

Jon Meyersinstructor
~ 2 years ago

Just a heads up, "Function Hooks" has been renamed "Database Webhooks". Same function, just a new name πŸ‘

~ 2 years ago

Hi Jon,

Is there another way to do this without using hooks ? can we check if the profile just created after the login if its so call the create-stripe-customer from Login component?

cheers,