In the realm of online sales, the journey from seller to buyer is often marked by complexities. However, with the strategic fusion of Supabase and Lemon Squeezy, a new paradigm emerges, simplifying this journey for both parties.
How it works
To create a plot, a seller (X) puts their product for sale on Lemonsqueezy and a buyer (Y) purchases it. We use the Lemon Squeezy API KEY and a magic link form with Supabase to check if the buyer's email is linked to the purchase. If it is, a magic link is sent to the buyer's email for product access.
Create your store
To create your store, go to Lemon Squeezy. Follow the steps explained there to add your first product. Get the API KEY by going to Settings > API tab from the Store panel and creating one.
Create project
npx create-next-app@latest
Add environment variables
LEMON_SQUEEZY_KEY=
NEXT_PUBLIC_SUPABASE_URL=
NEXT_PUBLIC_SUPABASE_ANON_KEY=
After creating the project and adding environment variables, include Supabase.
npm i @supabase/ssr @supabase/supabase-js
Include the following files in lib > supabase in the main directory of your project.
Now I will create a magic link form and then the API file. Then we will include the pages that will be shown to the user who will log in with the magic link.
magic-link-form.tsx
The code takes user input in the form of an email address and sends it to /api/customers?email= to check if the user has made any purchases through the Lemon Squeezy API.
app/api/customers/route.ts
- created BASE_URL for the domain, but you can create separate keys for both localhost and production in your `.env.local` file and use them, it will be much better.
- used the searchParams property to access the user's form input.
- Through the Lemon Squeezy API, I check if the user is a customer with the email address they typed in.
- When a customer was present, I utilized Supabase's signInWithOtp feature to send them a magic link.
The email template sent to the user does not have the information we want by default. For this, I need to edit the Magic link content as follows.
<h2>Magic Link</h2>
<p>Follow this link to login:</p>
<p><a href="{{ .SiteURL }}/auth/confirm?token_hash={{ .TokenHash }}&type=magiclink">Log In</a></p>
After completing these steps, we can now move on to the next step. The user who clicks on the Log In button in the email will be directed to the verification page. Now I am going to build this verification page.
app/auth/confirm/route.ts
- get token_hash and type information from the user.
- If this information is available, a cookie is set for the user.
- If verifyOtp property has no error, redirectTo function is executed and user is redirected to relevant page.
After that, you can now offer your logged-in user the opportunity to view any page and content using Supabase stuffs.
While writing this post I was inspired by @emilkowalski's story of creating the Video course platform project. I would like to express my gratitude to him. Check out his project.