Next.js Discord

Discord Forum

CSRFToken not work with Next JS 14 server components

Unanswered
Barbary Lion posted this in #help-forum
Open in Discord
Barbary LionOP
I used Next JS 14 with the Django backend and tried to implement user session authentication. I had an issue to attach the X-CSRFToken to the header when do the fetch.
The X-CSRFToken was attached to header when I use client component but not with server action.
Can any tell how to make this work with server action ?
Code below works with client.
`const login = (event) =>{
event.preventDefault();
fetch("/api/login/", {
cache: 'no-cache',
method: "POST",
headers: {
"Content-Type": "application/json",
"X-CSRFToken": cookies.get('csrftoken')
},
credentials: "same-origin",
body: JSON.stringify({username:"", password: ""}),
})``

0 Replies