Skip to main content

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

  1. User authenticates via Supabase (OAuth, email/password, or magic link)
  2. Session is established and stored in cookies
  3. Middleware validates session and injects user ID/email into request headers
  4. Server actions use getUserId() or getUserEmail() to access authenticated user data
import { getUserId } from '@/server/auth'

export async function myServerAction() {
  const userId = await getUserId() // Throws if not authenticated
  // Perform authenticated operation
}

Error Handling

Server actions use try-catch blocks and throw meaningful errors:
try {
  // Perform operation
} catch (error) {
  console.error('Operation failed:', error)
  throw error
}

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:
import { revalidatePath, revalidateTag } from 'next/cache'

// Revalidate specific path
revalidatePath('/')

// Revalidate by tag
revalidateTag(`trades-${userId}`)

Database Connection

Server actions use Prisma with PostgreSQL adapter for optimal performance:
import { prisma } from '@/lib/prisma'

const users = await prisma.user.findMany()

Best Practices

Every server action should verify the user has permission to perform the operation.
const userId = await getUserId()
const account = await prisma.account.findFirst({
  where: { id: accountId, userId }
})
if (!account) throw new Error('Account not found')
Provide meaningful error messages and log errors for debugging.
try {
  // Operation
} catch (error) {
  console.error('Context:', error)
  throw new Error('User-friendly message')
}
For operations in try-catch blocks, use finally to ensure cleanup.
try {
  // Database operations
} finally {
  await prisma.$disconnect()
}

Next Steps

Authentication Functions

Learn about sign-in, sign-up, and user management

Trade Operations

Explore trade management functions

Build docs developers (and LLMs) love