<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Jonathan Diniz</title>
    <description>The latest articles on DEV Community by Jonathan Diniz (@jonathan_diniz_cee738f10e).</description>
    <link>https://dev.to/jonathan_diniz_cee738f10e</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3953073%2Fb630c570-ca8e-4167-88fe-e6079dfd1c51.png</url>
      <title>DEV Community: Jonathan Diniz</title>
      <link>https://dev.to/jonathan_diniz_cee738f10e</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jonathan_diniz_cee738f10e"/>
    <language>en</language>
    <item>
      <title>How I wired Stripe subscriptions to Supabase in Next.js 15 (the parts tutorials skip)</title>
      <dc:creator>Jonathan Diniz</dc:creator>
      <pubDate>Tue, 26 May 2026 18:32:32 +0000</pubDate>
      <link>https://dev.to/jonathan_diniz_cee738f10e/how-i-wired-stripe-subscriptions-to-supabase-in-nextjs-15-the-parts-tutorials-skip-2b9l</link>
      <guid>https://dev.to/jonathan_diniz_cee738f10e/how-i-wired-stripe-subscriptions-to-supabase-in-nextjs-15-the-parts-tutorials-skip-2b9l</guid>
      <description>&lt;p&gt;Every SaaS needs the same foundation: auth, payments, a database, protected routes. I've built this from scratch too many times. This post covers the parts that actually trip people up — not the happy path, but the edge cases that break production apps.&lt;/p&gt;

&lt;p&gt;The three Supabase client problem&lt;br&gt;
Most tutorials show one Supabase client. In a Next.js 15 App Router project you need three:&lt;/p&gt;

&lt;p&gt;// lib/supabase/client.ts — browser only (Client Components)&lt;br&gt;
import { createBrowserClient } from "@supabase/ssr";&lt;/p&gt;

&lt;p&gt;export function createClient() {&lt;br&gt;
  return createBrowserClient(&lt;br&gt;
    process.env.NEXT_PUBLIC_SUPABASE_URL!,&lt;br&gt;
    process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!&lt;br&gt;
  );&lt;br&gt;
}&lt;br&gt;
// lib/supabase/server.ts — Server Components + API routes&lt;br&gt;
import { createServerClient } from "@supabase/ssr";&lt;br&gt;
import { cookies } from "next/headers";&lt;/p&gt;

&lt;p&gt;export async function createClient() {&lt;br&gt;
  const cookieStore = await cookies();&lt;br&gt;
  return createServerClient(&lt;br&gt;
    process.env.NEXT_PUBLIC_SUPABASE_URL!,&lt;br&gt;
    process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,&lt;br&gt;
    {&lt;br&gt;
      cookies: {&lt;br&gt;
        getAll() { return cookieStore.getAll(); },&lt;br&gt;
        setAll(cookiesToSet) {&lt;br&gt;
          try {&lt;br&gt;
            cookiesToSet.forEach(({ name, value, options }) =&amp;gt;&lt;br&gt;
              cookieStore.set(name, value, options)&lt;br&gt;
            );&lt;br&gt;
          } catch {}&lt;br&gt;
        },&lt;br&gt;
      },&lt;br&gt;
    }&lt;br&gt;
  );&lt;br&gt;
}&lt;br&gt;
// lib/supabase/service.ts — webhooks only (bypasses RLS)&lt;br&gt;
import { createClient } from "@supabase/supabase-js";&lt;/p&gt;

&lt;p&gt;export function createServiceClient() {&lt;br&gt;
  return createClient(&lt;br&gt;
    process.env.NEXT_PUBLIC_SUPABASE_URL!,&lt;br&gt;
    process.env.SUPABASE_SERVICE_ROLE_KEY!&lt;br&gt;
  );&lt;br&gt;
}&lt;br&gt;
Why three? The browser client handles auth state on the client side. The server client reads cookies from the Next.js request context and refreshes sessions. The service client uses the service role key — it bypasses Row Level Security entirely, which is what you need in webhooks where there's no authenticated user.&lt;/p&gt;

&lt;p&gt;Using the wrong client in the wrong context causes subtle bugs: sessions that don't persist, RLS errors in webhooks, or auth state that doesn't update after login.&lt;/p&gt;

&lt;p&gt;Middleware that actually refreshes sessions&lt;br&gt;
Route protection is easy. Session refresh is the part people miss:&lt;/p&gt;

&lt;p&gt;// middleware.ts&lt;br&gt;
import { createServerClient } from "@supabase/ssr";&lt;br&gt;
import { NextResponse, type NextRequest } from "next/server";&lt;/p&gt;

&lt;p&gt;export async function middleware(request: NextRequest) {&lt;br&gt;
  let supabaseResponse = NextResponse.next({ request });&lt;/p&gt;

&lt;p&gt;const supabase = createServerClient(&lt;br&gt;
    process.env.NEXT_PUBLIC_SUPABASE_URL!,&lt;br&gt;
    process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,&lt;br&gt;
    {&lt;br&gt;
      cookies: {&lt;br&gt;
        getAll() { return request.cookies.getAll(); },&lt;br&gt;
        setAll(cookiesToSet) {&lt;br&gt;
          cookiesToSet.forEach(({ name, value }) =&amp;gt;&lt;br&gt;
            request.cookies.set(name, value)&lt;br&gt;
          );&lt;br&gt;
          supabaseResponse = NextResponse.next({ request });&lt;br&gt;
          cookiesToSet.forEach(({ name, value, options }) =&amp;gt;&lt;br&gt;
            supabaseResponse.cookies.set(name, value, options)&lt;br&gt;
          );&lt;br&gt;
        },&lt;br&gt;
      },&lt;br&gt;
    }&lt;br&gt;
  );&lt;/p&gt;

&lt;p&gt;// This line refreshes the session — don't skip it&lt;br&gt;
  const { data: { user } } = await supabase.auth.getUser();&lt;/p&gt;

&lt;p&gt;if (!user &amp;amp;&amp;amp; request.nextUrl.pathname.startsWith("/dashboard")) {&lt;br&gt;
    return NextResponse.redirect(new URL("/login", request.url));&lt;br&gt;
  }&lt;/p&gt;

&lt;p&gt;if (user &amp;amp;&amp;amp; (request.nextUrl.pathname === "/login" ||&lt;br&gt;
               request.nextUrl.pathname === "/signup")) {&lt;br&gt;
    return NextResponse.redirect(new URL("/dashboard", request.url));&lt;br&gt;
  }&lt;/p&gt;

&lt;p&gt;return supabaseResponse;&lt;br&gt;
}&lt;br&gt;
The critical part: supabase.auth.getUser() in middleware doesn't just check the session — it also refreshes the access token if it's expired and writes the updated cookie back to the response. Without this call, users get logged out unexpectedly after the token TTL.&lt;/p&gt;

&lt;p&gt;The Stripe webhook that actually syncs to Supabase&lt;br&gt;
This is where most implementations fail. The webhook handler needs to:&lt;/p&gt;

&lt;p&gt;Verify the signature (not optional)&lt;br&gt;
Handle idempotency — Stripe delivers webhooks at least once, sometimes more&lt;br&gt;
Update the correct tables&lt;br&gt;
// app/api/stripe/webhook/route.ts&lt;br&gt;
import { stripe } from "@/lib/stripe";&lt;br&gt;
import { createServiceClient } from "@/lib/supabase/service";&lt;br&gt;
import type Stripe from "stripe";&lt;/p&gt;

&lt;p&gt;export async function POST(request: Request) {&lt;br&gt;
  const body = await request.text();&lt;br&gt;
  const sig = request.headers.get("stripe-signature");&lt;/p&gt;

&lt;p&gt;if (!sig) return new Response("No signature", { status: 400 });&lt;/p&gt;

&lt;p&gt;let event: Stripe.Event;&lt;br&gt;
  try {&lt;br&gt;
    event = stripe.webhooks.constructEvent(&lt;br&gt;
      body,&lt;br&gt;
      sig,&lt;br&gt;
      process.env.STRIPE_WEBHOOK_SECRET!&lt;br&gt;
    );&lt;br&gt;
  } catch {&lt;br&gt;
    return new Response("Invalid signature", { status: 400 });&lt;br&gt;
  }&lt;/p&gt;

&lt;p&gt;// Service client — no authenticated user in webhooks&lt;br&gt;
  const supabase = createServiceClient();&lt;/p&gt;

&lt;p&gt;switch (event.type) {&lt;br&gt;
    case "customer.subscription.created":&lt;br&gt;
    case "customer.subscription.updated": {&lt;br&gt;
      const sub = event.data.object as Stripe.Subscription;&lt;br&gt;
      const userId = sub.metadata.supabase_user_id;&lt;br&gt;
      if (!userId) break;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  const priceId = sub.items.data[0]?.price.id;
  const tier = priceId === process.env.STRIPE_PRICE_PRO_MONTHLY
    ? "pro"
    : "starter";

  // Update the user's tier and status
  await supabase
    .from("profiles")
    .update({
      subscription_status: sub.status as "active" | "canceled" | "past_due" | "trialing",
      subscription_tier: tier,
    })
    .eq("id", userId);

  // Upsert — not insert — so duplicate webhooks don't error
  await supabase.from("subscriptions").upsert({
    user_id: userId,
    stripe_subscription_id: sub.id,
    stripe_customer_id: sub.customer as string,
    status: sub.status,
    price_id: priceId,
    current_period_start: new Date(sub.current_period_start * 1000).toISOString(),
    current_period_end: new Date(sub.current_period_end * 1000).toISOString(),
  }, { onConflict: "stripe_subscription_id" });
  break;
}

case "customer.subscription.deleted": {
  const sub = event.data.object as Stripe.Subscription;
  const userId = sub.metadata.supabase_user_id;
  if (!userId) break;
  await supabase
    .from("profiles")
    .update({ subscription_status: "canceled", subscription_tier: "free" })
    .eq("id", userId);
  break;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;return Response.json({ received: true });&lt;br&gt;
}&lt;br&gt;
Two things to note:&lt;/p&gt;

&lt;p&gt;upsert not insert: Stripe guarantees at-least-once delivery. If you use insert, the second delivery throws a unique constraint error. upsert with onConflict handles duplicates cleanly.&lt;/p&gt;

&lt;p&gt;supabase_user_id in metadata: When creating the checkout session, pass the user's ID in the subscription metadata. This is how you link a Stripe subscription back to a Supabase user — don't rely on email matching, it's fragile.&lt;/p&gt;

&lt;p&gt;// In your checkout route — set the metadata at creation time&lt;br&gt;
const session = await stripe.checkout.sessions.create({&lt;br&gt;
  customer: customerId,&lt;br&gt;
  mode: "subscription",&lt;br&gt;
  line_items: [{ price: priceId, quantity: 1 }],&lt;br&gt;
  subscription_data: {&lt;br&gt;
    metadata: { supabase_user_id: user.id } // ← this is the link&lt;br&gt;
  },&lt;br&gt;
  success_url: &lt;code&gt;${process.env.NEXT_PUBLIC_APP_URL}/dashboard/billing?success=true&lt;/code&gt;,&lt;br&gt;
  cancel_url: &lt;code&gt;${process.env.NEXT_PUBLIC_APP_URL}/dashboard/billing&lt;/code&gt;,&lt;br&gt;
});&lt;br&gt;
Database schema: auto-profile on signup&lt;br&gt;
The cleanest pattern: a Postgres trigger that creates the profile row the moment a user signs up, so you never have to handle the "profile doesn't exist yet" case in your app code.&lt;/p&gt;

&lt;p&gt;-- Auto-create profile on signup&lt;br&gt;
create or replace function public.handle_new_user()&lt;br&gt;
returns trigger as $$&lt;br&gt;
begin&lt;br&gt;
  insert into public.profiles (id, email, full_name, avatar_url)&lt;br&gt;
  values (&lt;br&gt;
    new.id,&lt;br&gt;
    new.email,&lt;br&gt;
    new.raw_user_meta_data-&amp;gt;&amp;gt;'full_name',&lt;br&gt;
    new.raw_user_meta_data-&amp;gt;&amp;gt;'avatar_url'&lt;br&gt;
  );&lt;br&gt;
  return new;&lt;br&gt;
end;&lt;br&gt;
$$ language plpgsql security definer;&lt;/p&gt;

&lt;p&gt;create trigger on_auth_user_created&lt;br&gt;
  after insert on auth.users&lt;br&gt;
  for each row execute procedure public.handle_new_user();&lt;br&gt;
security definer is required here — the trigger runs with the permissions of the function owner (postgres), not the calling user. Without it, the insert into public.profiles fails because the new user doesn't have permission to insert their own row yet (RLS hasn't been evaluated in time).&lt;/p&gt;

&lt;p&gt;RLS: the minimum you need&lt;br&gt;
alter table public.profiles enable row level security;&lt;br&gt;
alter table public.subscriptions enable row level security;&lt;/p&gt;

&lt;p&gt;-- Users can only read and update their own profile&lt;br&gt;
create policy "Users can view own profile" on public.profiles&lt;br&gt;
  for select using (auth.uid() = id);&lt;/p&gt;

&lt;p&gt;create policy "Users can update own profile" on public.profiles&lt;br&gt;
  for update using (auth.uid() = id);&lt;/p&gt;

&lt;p&gt;-- Users can only read their own subscriptions&lt;br&gt;
create policy "Users can view own subscriptions" on public.subscriptions&lt;br&gt;
  for select using (auth.uid() = user_id);&lt;br&gt;
No insert policy on profiles — the trigger handles that with security definer. No delete policy — users shouldn't delete their own profile from the app. The webhook uses the service client which bypasses RLS entirely.&lt;/p&gt;

&lt;p&gt;Wrapping up&lt;br&gt;
These are the patterns that took me the longest to get right:&lt;/p&gt;

&lt;p&gt;Three Supabase clients for different contexts&lt;br&gt;
Middleware that refreshes sessions, not just checks them&lt;br&gt;
Idempotent webhook handler with upsert&lt;br&gt;
Subscription metadata linking Stripe to Supabase&lt;br&gt;
security definer trigger for auto-profile creation&lt;br&gt;
I packaged all of this (plus a dashboard, landing page, and billing page) into a boilerplate called ShipNext: &lt;a href="https://dinizjo.gumroad.com/l/xodcmf" rel="noopener noreferrer"&gt;https://dinizjo.gumroad.com/l/xodcmf&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Pay what you want for the next 48h. MIT license, 27 files, README gets you deployed on Vercel in ~30 minutes.&lt;/p&gt;

&lt;p&gt;Questions about any of the patterns above — happy to go deeper in the comments.&lt;/p&gt;

</description>
      <category>nextjs</category>
      <category>supabase</category>
      <category>stripe</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
