salesforce / tough-cookie

RFC6265 Cookies and CookieJar for Node.js
BSD 3-Clause "New" or "Revised" License
964 stars 207 forks source link

Methods don't show up #415

Closed brunonevesy closed 4 months ago

brunonevesy commented 4 months ago

The methods removeCookie never show up, below is the code:

async function Funcao() { try {

    const tough = require('tough-cookie'); // tough-cookie
    const cookieJar = new tough.CookieJar();

    cookieJar.setCookieSync('did=XPTO1; Max-Age=31557600; Path=/; Expires=Sat, 31 May 2025 01:40:02 GMT; HttpOnly; Secure; SameSite=None', 'https://ABC.com');
    cookieJar.setCookieSync('did_compat=XPTO2; Max-Age=31557600; Path=/; Expires=Sat, 31 May 2025 01:40:02 GMT; HttpOnly; Secure', 'https://ABC.com');
    cookieJar.setCookieSync('__cf_bm=XPTO3; path=/; expires=Thu, 30-May-24 20:10:02 GMT; domain=.eu.ABC.com; HttpOnly; Secure; SameSite=None', 'https://ABC.com');

    var cookie = cookieJar.getCookies('https://ABC.com');

    if (cookie) {

        cookieJar.store.removeCookie(cookie.domain, cookie.path, cookie.key, (err) => {
            if (err) {
                console.error('Erro ao remover cookie:', err);
                return;
            }
            console.log(`Cookie removido com sucesso`);
        })

    }

    console.log("breakpoint");

} catch (error) {
    console.log(error)
} finally {

}

}Funcao()

wjhsf commented 4 months ago

The reason you aren't seeing the callback being called is because the following line causes an error to be thrown before removeCookie is called.

cookieJar.setCookieSync('__cf_bm=XPTO3; path=/; expires=Thu, 30-May-24 20:10:02 GMT; domain=.eu.ABC.com; HttpOnly; Secure; SameSite=None', 'https://ABC.com');
brunonevesy commented 4 months ago

The reason you aren't seeing the callback being called is because the following line causes an error to be thrown before removeCookie is called.

cookieJar.setCookieSync('__cf_bm=XPTO3; path=/; expires=Thu, 30-May-24 20:10:02 GMT; domain=.eu.ABC.com; HttpOnly; Secure; SameSite=None', 'https://ABC.com');

Hi,

Thank you so much for your reply

I remove that cookie but i still have the problem, i have more than one cookie in var cookie = cookieJar.getCookies('https://ABC.com'); but after in:

cookieJar.store.removeCookie(cookie.domain, cookie.path, cookie.key, (err)

i can't acess the cookie array

Can you help me?

Thank you so much

colincasey commented 4 months ago

@brunonevesy, For the following code:

var cookie = cookieJar.getCookies('https://ABC.com');

Because you aren't providing a callback, the value of cookie will be a Promise<Cookie[]> (see docs), not an array of cookies.

But once you get the array of cookies back, the code you provided below still won't work unless you make significant changes:

// this check is not necessary, the result of successfully retrieving cookies is always Cookie[]
if (cookie) {
  // the cookies are not being iterated over, so `cookie` is currently an array value 
  // which doesn't have properties named `domain` or `path` or `key`
  cookieJar.store.removeCookie(cookie.domain, cookie.path, cookie.key, (err) => {
    if (err) {
      console.error('Erro ao remover cookie:', err);
      return;
    }
    console.log(`Cookie removido com sucesso`);
  })  
}
// the above code is callback-based (async) so if you want this console.log output to show up 
// after all the cookies have been removed that will likely not happen in the order you expect
// unless you add code to wait for all the callbacks to complete
console.log("breakpoint");