tenants
table:
.env.local.example
to .env.local
, and update it with your workspace and database name.
(Your workspace and database name are displayed in the header of the Nile dashboard.)
Also fill in the username and password with the credentials you picked up in the previous step.
It should look something like this:
node -v
.
Open http://localhost:3000 with your browser to see the result.
If all went well, your browser should show you the first page in the app, asking you to login or sign up.
After you sign up as a user of this example app, you’ll be able to see this user by going back to Nile Console and looking at the users table
4242 4242 4242 4242
),
and see the changes in the database:
app router
, so the application landing page is in app/page.tsx
.
We’ll start here and go over the code for managing tenant subscriptions.
/tenants
. You’ll find the code in app/tenants/page.tsx
.
The first thing we do in this page is to configure Nile SDK for the current user:
nile
that is used throughout the application.
This instance is a singleton, which you get by calling Nile SDK’s Server
method, which we are doing in ’@/lib/NileServer’
This is also where all the environment variables we’ve set earlier are being used, so lets take a look at this file:
basePath
configuration is the URL that nile
methods will call. This component calls Nile APIs directly, and therefore we set basePath
to NEXT_PUBLIC_NILE_API
.
So every page, route and function in our app can use the same nile
instance to access Nile APIs and DB.
But, we need to make sure we are using the right user and tenant context.
So we call configureNile
and pass in the cookies and the tenant ID.
After this point, we can use nile
to access the database and APIs, and it will use the right user and tenant context.
free
tier. This is done in app/tenants/tenant-actions.tsx
:
tier
parameter. This is because we want to create the tenant in the default tier, which is free
.
We handled that when we extended tenants
table with a column for the tenant tier:
app/tenants/[tenantid]/billing/page.tsx
:
createCheckoutSession
which is a NextJS server action implemented in app/tenants/[tenantid]/billing/checkout-actions.tsx
:
app/api/checkout-success/page.tsx
.
This is where we upgrade the tenant to a paid tier and store his customer and subscription references in our tenants
table:
tenants
table with the customer and subscription IDs, and upgrade the tenant to the basic
tier.customer_portal
API. This is implemented in app/tenants/[tenantid]/billing/checkout-actions.tsx
:
tenants
table, and then call Stripe’s API to create a portal session.
We then redirect the user to the portal session URL, which takes the user to the customer dashboard.
app/tenants/[tenantid]/billing/checkout-actions.tsx
:
cancel
API and cancel the subscription.
If this step succeeds, we downgrade the tenant in our database.