0

//server

app.get('/', cors(corsOptions),async ( req,res )=>{
    if (req.headers.cookie) {
        let ck = req.headers.cookie.split('=')
        const sendData = await mysql( 'list', ck[1] )
        res.json( sendData[0] )
    }else{
        res.json( {badRequest:'cookie is not defined'} )
    }
})

//client

export default function ApiData() {
    const [usr,setUsr] = useState([])

    useEffect(()=>{
        let headers = {
            'Access-Control-Allow-Origin':'*',
            'accept': 'application/json',
            'Cookie': document.cookie,
            'origin':'same-origin'
        }
        fetch('http://localhost:8080',{ headers: headers })
        .then(res => res.json())
        .then(res => setUsr(res))
        .then(res => console.log(res))
    },[])
    
    return(
        <div>
            {usr.badRequest}
            {usr.name}
        </div>
    )
}

//even with the cookie set, on the frontend, it does not send the headers to the backend and enters the else => condition (on the backend it shows that the cookie is undefined)

Jhow
  • 1
  • 2
  • https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#sending_a_request_with_credentials_included – niceman Aug 19 '22 at 18:26
  • You can't send your own cookies with `fetch()`. The spec does not allow it. And, they are specifically trying to prevent you from doing what you're doing which is sending some other domain's cookie to a different domain. Not allowed for all sorts of security reasons. – jfriend00 Aug 19 '22 at 18:30
  • in place of the cookie, I put 'key', it worked, thanks a lot – Jhow Aug 19 '22 at 19:08

0 Answers0