0

I am using React and I want to access to cookies, specifically accessToken, so that I can setLogin(true) based on existence of accessToken.

I tried document.cookies, react-cookies library, but I can't access to that cookies...

enter image description here

even though I can see cookies on the browser, whenever I console.log(cookies) like below, there is empty.

  1. When I used document.cookie

enter image description here;

  1. When I used react-cookies

enter image description here

enter image description here

and cors Options are like below

app.use(
  cors({
    origin: true,
    credentials: true,
    methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS', 'PATCH']
  })
);

How can I know there is cookie[accessToken] and

How can I use setLogin(true) based on accessToken

Nimantha
  • 6,405
  • 6
  • 28
  • 69
MarkCode
  • 3
  • 1

1 Answers1

0

The accesstoken is an httpOnly cookie. The httpOnly flag ensures that the cookie cannot be accessed by client-side javascript.

You can disable the httpOly flag on the accesstoken to make it accessible.

An alternative is getting the access token from the API and setting it in localstorage, which is the most common I've seen.

You can try and search online on how to store accesstoken in react app

Oluwasegun
  • 546
  • 4
  • 2