Posts

Showing posts with the label csrf protection

Using Server Actions in NextJS

Image
Introduction NextJS Server Actions is a powerful feature that allows you to perform data mutations directly on the server without needing to manually create API Routes, with key benefits including: Tight Integration with Forms: Works seamlessly with the HTML form action attribute. Progressive Enhancement: The application can still perform basic functions even if JavaScript is not fully loaded or is disabled. Security: Automatically protects against CSRF attacks and keeps sensitive processing logic on the server. Simplified Code: Reduces boilerplate code when connecting Client and Server. Detail First, let's create the app/todo/schema.ts file to define the types and validate data using Zod. import {z} from 'zod' export const TodoSchema = z. object ({ id : z. string (). optional (). nullable (), title : z. string (). min ( 1 , 'Title is required' ). max ( 100 ), }) export type Todo = z. infer < typeof TodoSchema > export type ActionState...