How to access the cookie from client service that is saved on server service

Issue

Actually I create a project on reactjs have two service. First is client service that is running on localhost port 3000 and second is server service that is running on port 8000. I created cookie on server service and wants to access from client service. But the cookie save on the sever port. How to save this cookie on client service or access the cookie from client service.

This is the code on the sever service

res.cookie("jwt",token,{expires:new Date(Date.now()+99999,
httpOnly: true
})

Solution

I fixed it by using the withCredentials property.

Axios Request from a different domain cannot set cookie values for their own domain unless withCredentials is set to true before making the request.

axios.get('some api url', {withCredentials: true});

I also included origin as true and credentials as true in cors


const corsOptions = {
    origin: true, //included origin as true
    credentials: true, //included credentials as true
};
app.use(cors(corsOptions));

Now it works fine for me. It send a response cookie to the client server.
see here

Answered By – Mohit Kumar

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published