vercel / nextjs-subscription-payments

Clone, deploy, and fully customize a SaaS subscription application with Next.js.
https://subscription-payments.vercel.app/
MIT License
5.83k stars 1.19k forks source link

Error with Stripe session termination - need to set NEXT_PUBLIC_SITE_URL? #208

Closed abwdev closed 1 year ago

abwdev commented 1 year ago

The Vercel deployment did not interact correctly with Stripe unless NEXT_PUBLIC_SITE_URL was set to the Vercel primary domain. Should NEXT_PUBLIC_SITE_URL be set in the environment variables as part of the configuration process? The readme does not mention this, however it was required for my install to interact with Stripe.

The issue occurred after deploying and testing a round trip with Stripe (after Stripe setup & products are read). The steps were:

  1. User logs into subscription site via primary domain
  2. Navigate to Stripe portal to purchase subscription
  3. Return back to subscription site
  4. Once returned to subscription site, the user is landed on the deployment specific domain (not the primary domain) and the login session is immediately terminated
  5. User must manually log in again which isn't good

I debugged helpers.ts and found that NEXT_PUBLIC_SITE_URL was not set to any value, and the code was therefore using NEXT_PUBLIC_VERCEL_URL as a backup. NEXT_PUBLIC_VERCEL_URL was set to the deployment specific domain that helpers.ts ended up using.

To fix this I went to the Vercel environment variables and set NEXT_PUBLIC_SITE_URL to be the primary domain. Stripe round trip completed without issue and session remained intact. Helpers.ts was checked & found to now use the primary domain.

Is there a config activity earlier in the setup that I missed that would ensure NEXT_PUBLIC_SITE_URL is automatically populated? If not the docs should probably get updated to ensure it's correctly set.