Cookies Reader
Cookies Reader
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
*
* @description CookiesReader
* @augments
* @example
*
*/
const CookiesReader = (key = ``, debug = false) => {
if(key) {
let name = `${key}=`;
let decodedCookie = decodeURIComponent(document.cookie);
let cookies = decodedCookie.split(`;`);
for(let i = 0; i <cookies.length; i++) {
let cookie = cookies[i];
while (cookie.charAt(0) === ` `) {
// trim() ???
cookie = cookie.substring(1);
}
if (cookie.indexOf(name) === 0) {
return cookie.substring(name.length, cookie.length);
}
}
return "";
} else {
console.error(`Please pass the key of cookie!, key = ${key}`);
}
};
const getCookieValue = (key = ``, debug = false) => {
if(key) {
let name = `${key}=`;
let decodedCookie = decodeURIComponent(document.cookie);
let cookies = decodedCookie.split(`;`);
for(let i = 0; i <cookies.length; i++) {
let cookie = cookies[i];
while (cookie.charAt(0) === ` `) {
// trim() ???
cookie = cookie.substring(1);
}
if (cookie.indexOf(name) === 0) {
return cookie.substring(name.length, cookie.length);
}
}
return "";
} else {
console.error(`Please pass the key of cookie!, key = ${key}`);
}
};
export default CookiesReader;
export {
CookiesReader,
getCookieValue,
};
/*
import {
getCookieValue,
// CookiesReader,
} from "./cookies-reader";
JSON.parse(decodeURIComponent(atob(getCookieValue(`access_token_test`).split(`.`)[1])));
JSON.parse(decodeURIComponent(atob(getCookieValue(`access_token_prod`).split(`.`)[1])));
JSON.parse(decodeURIComponent(atob(getCookieValue(`${access_token}`).split(`.`)[1])));
*/
demo
import {
getCookieValue,
// CookiesReader,
} from "./cookies-reader";
JSON.parse(decodeURIComponent(atob(getCookieValue(`access_token_test`).split(`.`)[1])));
JSON.parse(decodeURIComponent(atob(getCookieValue(`access_token_prod`).split(`.`)[1])));
import {
getCookieValue,
// CookiesReader,
} from "./cookies-reader";
import {
ROLE,
DEV
} from "./url-roles";
let access_token = ``;
if (DEV === "production") {
access_token = `access_token_prod`;
} else {
access_token = `access_token_test`;
}
// let token = JSON.parse(decodeURIComponent(atob(document.cookie.replace(`${access_token}`, ``).split(`.`)[1])));
let token = JSON.parse(decodeURIComponent(atob(getCookieValue(`${access_token}`).split(`.`)[1])));
HttpOnly & bug
bug
const CookiesReader = (key = ``, debug = false) => {
if(key) {
let name = `${key}=`;
let decodedCookie = decodeURIComponent(document.cookie);
let cookies = decodedCookie.split(`;`);
for(let i = 0; i <cookies.length; i++) {
let cookie = cookies[i];
while (cookie.charAt(0) === ` `) {
// trim() ???
cookie = cookie.substring(1);
}
if (cookie.indexOf(name) === 0) {
return cookie.substring(name.length, cookie.length);
}
}
return "";
} else {
console.error(`Please pass the key of cookie!, key = ${key}`);
}
};
CookiesReader(`JSESSIONID`);
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/9816422.html
未经授权禁止转载,违者必究!