I am running into a problem where I have to refresh my express endpoint to be able to read req.headers.cookie If I do not refresh the endpoint then console.log(req.headers.cookie) returns undefined instead of the assigned value. I have included some photos below to better show you what my problem is I have also included my back-end code that sets the cookie and should also console.log(req.headers.cookie) Thanks in advance for any help!
Images:
First time I hit the Login button this is the cookie I get.

Seconed time I hit the login button this is the cookie I get.

Notice below on our second back-end response how the AccessToken value is equal to the first response we received on our front-end. Almost as if our backend code is always reading the previous value of req.headers.cookie instead of the current value.

Back-End Code:
app.get('/', (req, res) => {
try {
res.cookie("AccessToken", globalToken, {
sameSite: 'strict',
maxAge: ((((1000 * 60) * 60) * 24) * 7), /* expire a week from today */
httpOnly: true,
path: '/'
// secure: true
});
res.send();
} catch (error) {
console.log(error)
} finally {
console.log(req.headers.cookie);
frontendCookie = req.headers.cookie;
}
});
HTTP works by having a client (like a browser) make a request. The server then sends a response.
If the response tells the client to store cookies, then they will be sent back to the server on subsequent requests.
So the order of events is:
You seem to be expecting the first request to be modified with the cookies that won't exist until the first response is processed.
If you want to take data from either cookies or elsewhere, then you need logic more along the liens of:
let access_token = req.headers.cookie;
if (some condition to set a new access token) {
access_token = get_new_access_token_value();
res.cookie( arts to set to access_token );
}
do_something_with(access_token);