Overview
Server actions provide type-safe, server-side data mutations using Next.js App Router. All actions are located in the/actions directory and use the "use server" directive.
Action Response Type
All server actions return a consistent response type:User Management Actions
get-user.ts
Retrieves the authenticated user from Supabase.create-user.ts
Creates a new user record and associated Dodo Payments customer.- Get authenticated user from Supabase
- Check if user exists in database
- Create Dodo Payments customer
- Insert user record with customer ID
delete-account.ts
Soft deletes a user account.Payment Actions
create-dodo-customer.ts
Creates a customer in Dodo Payments.get-products.ts
Fetches all available products from Dodo Payments.get-invoices.ts
Retrieves payment history for the current user.Subscription Actions
get-user-subscription.ts
Retrieves user details and current subscription.- User database record
- Current subscription (null for free tier)
cancel-subscription.ts
Schedules subscription cancellation at next billing date.restore-subscription.ts
Cancels a scheduled subscription cancellation.change-plan.ts
Changes the subscription plan with immediate proration.- Immediate plan change
- Prorated billing
- Automatic charge/credit calculation
Error Handling Pattern
All actions follow a consistent error handling pattern:Usage in Components
Server actions are called from client components:Best Practices
- Always check authentication - Use
getUser()to verify user is logged in - Type safety - Use proper TypeScript types for parameters and return values
- Error handling - Always wrap in try/catch and return structured responses
- Database sync - Update both Dodo Payments and local database for consistency
- Transaction safety - Consider database transactions for multi-step operations
