Cookies can only be modified in a Server Actions or Route Handler
Unanswered
dilraba posted this in #help-forum
dilrabaOP
Hi guys, I using authjs with next-auth and nextjs app router for my project. And I am utilize axios for data fetching, and has interceptor to include token on each fetching request.
The things is, I want to logout when data fetching return 401, but it thrown error Cookies is can only be modified on Server Actions. My logout function is on the Server Actions
The things is, I want to logout when data fetching return 401, but it thrown error Cookies is can only be modified on Server Actions. My logout function is on the Server Actions
15 Replies
Share code.
dilrabaOP
Here it goes. I set 'use server' on top of the files. It doesn't work when I called either logout function or forceLogout
Try using “return”???
And you have to await it?
I will get out of bed and look at my code one sec
@Jboncz And you have to await it?
dilrabaOP
when I remove it still same error. so I just spew some code maybe it'll work
This is all I can give right now I didn’t strip any of the meaningless stuff out
I promise this works I used it earlier after my deployment to prod
Check back in and let me know that it works otherwise I can help more when I wake up
dilrabaOP
Can I call the loginAs (server action function) in another function that run on server component?
Hmmm no. I don’t think so server actions are strictly for client to server communication. At least that’s what use servers intended purpose is
dilrabaOP
got it. Your solutions helps a lot. I manage to solved this issue eventually. thanks