Home > Software design >  Cookie doesn't show as a client cookie. Javascript
Cookie doesn't show as a client cookie. Javascript

Time:11-26

I'm using a company's API.

When I log in, it sends me a cookie.

With the command below, I get this cookie.

const loginSysaidCookie = response.headers.get('set-cookie')

Then I give the console:

console.log("loginSysaidCookie with convert", loginSysaidCookie)

Console ->

loginSysaidCookie com convertida [ 'JSESSIONID=796330316C3EC071ED53338C108C4A99.inst15eu-autoscaleapp-002161; Path=/; Secure; HttpOnly; SameSite=Lax', 'SERVERID=inst15eu-autoscale-app0|Y3 Qq|Y3 Qq; path=/' ]

To send this cookie from the client to my API I am using. the command below:

res.cookie('JSESSIONID', loginSysaidCookie[0])
res.cookie('SERVERID', loginSysaidCookie[1])

But I still have this ->

'Cookie': 'JSESSIONID=JSESSIONID=D239C020B874EFEB3DD6DED7055DDC3B.inst15eu-autoscaleapp-002161; Path=/; Secure; HttpOnly; SameSite=Lax; SERVERID=SERVERID=inst15eu-autoscale-app0|Y3+TF|Y3+TF; path=/'

The client cookie looks like this ->>

'Cookie': 'JSESSIONID=85ABA2700058749DB748C3C7398B7667.inst15eu-autoscaleapp-002161; SERVERID=inst15eu-autoscale-app0|Y3 Bg|Y3 Bg'

-----------------------------

From Post Man cookies

JSESSIONID=85ABA2700058749DB748C3C7398B7667.inst15eu-autoscaleapp-002161; Path=/; Secure; HttpOnly;

SERVERID=inst15eu-autoscale-app0|Y3 Bg|Y3 Bg; Path=/;

My cookie looks like this -->

'Cookie': 'JSESSIONID=JSESSIONID=D239C020B874EFEB3DD6DED7055DDC3B.inst15eu-autoscaleapp-002161; Path=/; Secure; HttpOnly; SameSite=Lax; SERVERID=SERVERID=inst15eu-autoscale-app0|Y3+TF|Y3+TF; path=/'

------------------------------

From Post Man cookies

JSESSIONID=JSESSIONID=D239C020B874EFEB3DD6DED7055DDC3B.inst15eu-autoscaleapp-002161; Path=/; Secure; HttpOnly; SameSite=Lax; Path=/;

SERVERID=SERVERID=inst15eu-autoscale-app0|Y3+TF|Y3+TF; path=/; Path=/;

CodePudding user response:

Each entry in loginSysaidCookie is a "set cookie instruction" of the form

JSESSIONID=<value>; Path=<path>; Secure; HttpOnly; SameSite=<strict/lax/none>

To set this cookie in your response res, you must

res.cookie("JSESSIONID", "<value>", {
  path: "<path>",
  secure: true,
  httpOnly: true,
  sameSite: "<strict/lax/none>"
});

But you give the entire "set cookie instruction" as the value, that's why you see JSESSIONID=JSESSIONID....

If you don't want to parse the "set cookie instruction" into its constituent parts, you can simply

res.setHeader('Set-Cookie', loginSysaidCookie[0]);
res.setHeader('Set-Cookie', loginSysaidCookie[1]);

this works because the cookie you receive from your company's API contains neither a Domain attribute nor a Path other than /. If it did, you would probably have to change that attribute before sending the cookie from your server.

CodePudding user response:

@heiko-theißen

I used you code but I don't know why my cookie JSESSIONID don't comes on :(.

enter image description here

enter image description here

res.cookie("JSESSIONID", jsessionidWithoutKeyName, {
        path: "/",
        secure: true,
        httpOnly: true,
        sameSite: "none"
    });

    // este comeando seta o cookie
    res.setHeader('Set-Cookie', loginSysaidCookie[0]);
    res.setHeader('Set-Cookie', loginSysaidCookie[1]);

  • Related