Im trying to make a login in page on my project. Im using ReactJS for frontend and django for backend.
here's what the frontend look like
(1/2)
const fetchCSRF = async ()=>{
const url = 'http://localhost:8000/members/getCookie'
await fetch(url,{
method:"GET",
credentials:'include',
headers:{'Content-Type':'application/json'},
}).then(response =>{
return response.text()
}).then(data=>{
console.log(data)
return data
})
const csrfToken = getCookie('csrftoken')
return csrfToken
}
const getCookie = (name: string)=>{
let cookieValue = null;
if (document.cookie && document.cookie !== ''){
let cookies = document.cookie.split(';')
for (let i = 0; i < cookies.length; i++){
let cookie = cookies[i].trim();
if (cookie.substring(0, name.length + 1) === (name + '=')){
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break
}
}
}
if (cookieValue === null){
cookieValue = ''
}
console.log(cookieValue)
return cookieValue
}
async function confirmSignup(username: string, password: string, email: string){
const url = 'http://localhost:8000/members/signup'
const CSRFToken = await fetchCSRF()
fetch(url,{
method:"POST",
headers:{
'Content-Type':'application/json',
'X-CSRFToken': CSRFToken
},
body:JSON.stringify({
username:username,
email:email,
password:password,
})
}).then(response =>{
if(!response.ok){
throw new Error(response.statusText)
}
return response.text()
}).then(data=>{
console.log(data)
})
}