Best way to delete cookie in API route
Answered
Northeast Congo Lion posted this in #help-forum
Northeast Congo LionOP
Using fetch, I post to local /api route, I need to be able to delete user cookie from API route, is it possible?
Answered by Northeast Congo Lion
when i use this it says it can only be done from route handler or server action
14 Replies
@Clown https://nextjs.org/docs/app/api-reference/functions/cookies#cookiesdeletename
Northeast Congo LionOP
when i use this it says it can only be done from route handler or server action
Answer
Northeast Congo LionOP
i got a work around for my issue, but was very confused with above error lol
when you say api route, do you use pages dir?
@Northeast Congo Lion when i use this it says it can only be done from route handler or server action
Well you're gonna need to be more specific about the Next Version and the Router you are using, cause the API routes ARE Route Handlers in the App Router
(Though with more freedom and ease of use)
Northeast Congo LionOP
App router yes
Next 14^. I had api route which had post request inside. If post request returned bad http response I would delete cookies
The POST was executed but the cookies().delete was telling me can only be used in route handler
So I was super confused
In turn cookies were not deleted
Show the code of the route handler you are deleting the cookies in
Northeast Congo LionOP
Im just gonna mark this as closer. The code I had is gone as I had to implement a work sprung
Around