Add cookie to react lib
Closed this issue · 1 comments
paribaker commented
Cookies are needed for csrf I've seen us use this one
- create
utils/cookie.ts
export default function getCookie(cname: string): string {
const name = cname + '='
const decodedCookie = decodeURIComponent(document.cookie)
const ca = decodedCookie.split(';')
for (let i = 0; i < ca.length; i++) {
let c = ca[i]
while (c.charAt(0) === ' ') {
c = c.substring(1)
}
if (c.indexOf(name) === 0) {
return c.substring(name.length, c.length)
}
}
return ''
}
- Update
services/axios-instance.ts
to add the cookie to the header
import getCookie from '../utils/cookie'
const csrfToken = getCookie('csrftoken')
const axiosInstance = axios.create({
//baseUrl will be determined by the server proxy in vite.config.js
headers: {
'Content-Type': 'application/json',
'X-CSRFToken': csrfToken,
},
villagrat commented
React client code has been merged to main with this on this PR