Storing and retrieving cookie
Anyone successfully stored cookie in one route handler and retrieving it in another route handler?
14 Replies
I found a repo where someone set cookie and retrieve it in api route and consumed it successfully but when I implemented the same thing still couldn't achieve what he achieved I think the last version may have some bugs
if you mean getting the cookies from an incoming request, then yes, you can do that in route handler https://nextjs.org/docs/app/building-your-application/routing/route-handlers#cookies
if you want to store the contents on the server, then you'd need to store it yourself in a database or KV-store
Routing: Route Handlers
Create custom request handlers for a given route using the Web's Request and Response APIs.
What is a KV-store?
KV store is a kind of database with a key value pair
like a map or a hash map
I think I should use that instead of cookies could you provide a link please
a cookie is in the browser
kv store can be redis (like from upstash) or the vercel one (also upstash)
Yap but tried retrieving it when consuming an endpoint but shows null value but when I hit localhost:3000/api/cookie I get the value it's driving me crazy
Which one is the best?
they are the same
lmao
Then would you please direct on how to set it up if there's no any docs available
Upstash Documentation
Getting Started - Upstash Documentation
Create a Redis compatible database in seconds
Upstash Documentation
Next.js with Redis - Upstash Documentation
This tutorial shows how to use Upstash inside your Next.js application.
Thanks a lot let me give it a try because I'm tired of cookies been stack for 5 days
Can I install the package without a fresh initialization of project
yeah
Thanks I will be back in case anything goes wrong
Can I get more information on how to use redis as I only see one example
Wow Redis works like a charm