简体   繁体   中英

Cannot set the expire date of a cookie

I'm developing an web application with ReactJS and a Django web API. My problem is:

I send a request to the API, that returns me a token, that I'll use to send requests that needs authentication, and a expire date for the token.

The response is something like:

{
   name: "Elliot",
   id: 1,
   token: "<the token here>"
   expires: "2018-04-29T17:00:00.000Z"
}

Then, when I try to do this:

document.cookie = `token=${response.token};expires=${response.expires}`

It sets the expire date to the year of 1969. Does anyone know why?

Details:

  • I took a look if the date isn't already expired, but, it's not. It's always 3 hours from now.

Problem solved! I thought that the date was on the right format, but it wasn't. Then, I converted the date using toUTCString() , like this:

const expires = new Date(response.expires)
document.cookie = `token=${response.token};expires=${expires.toUTCString()}`

Hope it helps somebody else!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM