Skip to content

Instantly share code, notes, and snippets.

@MarcosSantosDev
Last active August 22, 2022 14:13
Show Gist options
  • Save MarcosSantosDev/4524673e580563f9ce8036750439ae49 to your computer and use it in GitHub Desktop.
Save MarcosSantosDev/4524673e580563f9ce8036750439ae49 to your computer and use it in GitHub Desktop.
Get cookies
/** Step 1
*
* Get cookies from a tab
*/
const getCookies = (keys = []) => {
const cookies = document.cookie;
const cookiesSplited = cookies.split('; ');
const myCookies = cookiesSplited.filter(cookie => {
const [key, value] = cookie.split('=');
const isEven = keys.includes(key);
return isEven;
}).map(cookie => `${cookie}; `)
return myCookies;
}
/** Step 2
*
* Example of use of the getCookies method
*/
const myCookies = getCookies(['access_token', 'refresh_token']);
// [...] Copy the values returned by the `getCookies(...)` function, and in a new tab follow the steps below.
/** Step 3
*
* Method to set new cookies
*/
const setCookie = (cookieParam) => document.cookie = cookieParam + ' path=/;';
/** Step 4
*
* Using the results obtained with the `getCookies` method in the `.map` and creating them in a new tab with a new domain
*/
myCookies.map(cookie => setCookie(cookie))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment