Cookies in Route Handlers

Cookies are essential part of them and have variety of application ranging from storing user preferences to stores auth tokens and all. So in this post we are going to explore How to get the cookies values and also how to set values and delete them in server component, client component and in api route handlers.

Server Components

Server components allows you to perform actions without needing to make an api route. So you can basically fetch data directly into server components and also experimental support for handling form. We will set the auth token first using the experimental server actions then we will get the values

Setting cookie

For setting cookie we will make use of server actions. So go to page.tsx and let’s firstly import the cookies function

import {cookies} from "next/headers"

Session Management: Managing user sessions for activities like logins and shopping carts.

Personalization: Storing user preferences and themes.

Tracking: Recording and analyzing user behavior.

https://nextjs.org/docs/app/api-reference/functions/cookies

Leave a comment

Your email address will not be published. Required fields are marked *