Overview
Deltalytix uses Next.js Server Actions for all backend operations. Server actions provide a secure, type-safe way to perform server-side operations directly from client components without the need for separate API routes.What are Server Actions?
Server Actions are asynchronous functions that execute on the server. They are defined in files marked with the'use server' directive and can be called directly from client components.
Key Benefits
- Type Safety: Full TypeScript support with automatic type inference
- Security: Server-only code that never exposes sensitive logic to the client
- Simplicity: No need to create separate API routes
- Performance: Optimized for minimal payload size
Authentication
All server actions in Deltalytix require user authentication. The authentication system is built on:- Supabase Auth: For user authentication and session management
- Prisma: For database operations and user data management
- Middleware: Optimized authentication checks that inject user data into headers
Authentication Flow
- User authenticates via Supabase (OAuth, email/password, or magic link)
- Session is established and stored in cookies
- Middleware validates session and injects user ID/email into request headers
- Server actions use
getUserId()orgetUserEmail()to access authenticated user data
Error Handling
Server actions use try-catch blocks and throw meaningful errors:Available Server Action Groups
Authentication
User authentication, sign-in, sign-up, and identity management
Trades
Trade CRUD operations, tagging, and image management
Accounts
Account management, metrics calculation, and payout tracking
Subscriptions
Subscription management and billing operations
Cache Revalidation
Many server actions trigger cache revalidation to ensure UI updates:Database Connection
Server actions use Prisma with PostgreSQL adapter for optimal performance:Best Practices
Always validate user permissions
Always validate user permissions
Every server action should verify the user has permission to perform the operation.
Handle errors gracefully
Handle errors gracefully
Provide meaningful error messages and log errors for debugging.
Disconnect Prisma when done
Disconnect Prisma when done
For operations in try-catch blocks, use finally to ensure cleanup.
Next Steps
Authentication Functions
Learn about sign-in, sign-up, and user management
Trade Operations
Explore trade management functions