Stripe Subscriptions in ShadcnKit
shadcnkit provides a foundation for building applications with subscription-based access control and resource limits.
Let’s dive into the details of how it works and how you can extend it to fit your needs.
Overview
shadcnkit consists of several key components:
- Subscription Plans: Defined in
constants/plans.ts
, this file contains the available subscription plans and their associated resource limits. - Middleware: Located in
middlewares/subscriptionCheck.ts
, the middleware checks the user’s subscription and enforces resource limits. - API Routes: The API routes in
app/api
handle CRUD operations for notes and tasks while utilizing the subscription middleware. - Database Service: The
utils/drizzle/notesTasksFoldersService.ts
file contains functions for interacting with the database, including usage tracking.
Adding New Subscription Plans
To add a new subscription plan, follow these steps:
- Open
constants/plans.ts
. - Add a new entry to the
PLANS
object with the desired plan name and resource limits. - If the plan is associated with a specific price ID, add a new entry to the
PRICE_ID_TO_PLAN
object, mapping the price ID to the plan name. - Save the file.
Example:
Adding New Resource Limits
To add new resource limits, follow these steps:
- Open
constants/plans.ts
. - Add a new property to each plan in the
PLANS
object, specifying the limit for the new resource. - Open
middlewares/subscriptionCheck.ts
. - Update the
SubscriptionLimits
interface to include the new resource limit. - Update the
resourceKeyToPlanLimitKey
object to map the new resource key to the corresponding plan limit key. - Save the files.
Example:
Adding New API Routes
To add a new API route, follow these steps:
- Create a new file in
app/api
for your resource (e.g.,app/api/newResource/route.ts
). - Implement the desired API route handlers (e.g.,
GET
,POST
,PUT
,DELETE
). - Import the
checkSubscription
middleware and apply it to the route handlers that require subscription checks. - Define the necessary subscription limits for each route handler.
- Implement the corresponding database service functions in
utils/drizzle/notesTasksFoldersService.ts
(or create a new service file for your resource). - Save the files.
Example:
How the Middleware Works
The checkSubscription
middleware in middlewares/subscriptionCheck.ts
performs the following steps:
- Authenticates the user using the
requireAuth
function. - Retrieves the user’s subscription details from the database using the
getSubscription
function. - Determines the user’s current subscription plan based on the price ID.
- Iterates over the provided
limits
object, which specifies the resource limits to check. - For each resource type, retrieves the current usage count from the database using the
getUsageTracking
function. - Compares the current usage count plus the increment value (if provided) against the plan’s limit for that resource type.
- If the usage exceeds the limit, returns a
403 Forbidden
response indicating that the plan limit has been reached. - If all checks pass, calls the provided
handler
function, passing the user ID. - Returns the response from the handler function.
The middleware is designed to be flexible and reusable across different API routes. You can apply it to any route handler that requires subscription-based access control.
Remember to update the corresponding database service functions and middleware configuration whenever you make changes to the subscription plans or resource limits.
If you have any further questions or need assistance, please don’t hesitate to reach out. Happy coding!