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`);


CookiesReader的更多相关文章

  1. cookie 详解 与 封装 实用的cookie

    在WEB前端开发中,cookie是必不可少的,网上也有很多开源的库可以使用,不过我还是想自己总结一下,做个笔记并封装一个实用的库.(1)什么是cookie? 从web 角度 cookie是用于存储信息 ...

随机推荐

  1. RMAN-06564错误的原因及解决办法

    今日在进行数据库恢复时,遭遇RMAN-06564错误,如下: RMAN> restore spfile from autobackup; Starting restore at 01-NOV-1 ...

  2. vijos P1629八 容斥原理

    https://vijos.org/p/1629 注意lcm要用LL 先给一个样例 1 2 1 10 思路.其实这题就是问,给定一堆数,要求不能整除其任意一个的数字有多少个. 容辞 + lcm dfs ...

  3. D. Winter Is Coming 贪心(好题)

    http://codeforces.com/contest/747/problem/D 大概的思路就是找到所有两个负数夹着的线段,优先覆盖最小的长度.使得那时候不用换鞋,是最优的. 但是这里有个坑点, ...

  4. hadoop-0.20.2完全分布式集群

    集群规划 准备五台台虚拟机(实验以五台RedHat Enterprise Linux 6.5为例) 防火墙.iptables.和SSH已经在上一篇说过在此就不再赘述,完全分布式相对于伪分布式多了几个注 ...

  5. Abp Framework中文文档上线

    感谢 ABP框架中国小组 给我们带来的ABP中文翻译,Web+为方便广大学习爱好者随时查阅,现推出了Gitbook风格的在线阅读文档:http://www.webplus.org.cn/documen ...

  6. math数学函数

    Console.WriteLine("Math.Sign(12)--->{0})", Math.Sign(12)) Console.WriteLine("math. ...

  7. 源代码管理SVN的使用

    SVN 全称是Subversion,集中式版本控制之王者 SVN 版本控制,需要自己搭建一个管理代码的服务器,提供开发人员,上传和下载 1.基本介绍 使用环境 要想利用SVN管理源代码,必须得有2套环 ...

  8. Leetcode_638.Shopping Offers

    https://leetcode.com/problems/shopping-offers/ In LeetCode Store, there are some kinds of items to s ...

  9. jQuery radio 选中提示

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. 一个圆的移动 AE教程 速度曲线调节

    AE里面速度的曲线调节 最终的小效果 两个关键点: 一:速度曲线调节 编辑速度图标,他的曲线是编辑速度的. 二:节点不要用贝塞尔曲线 编辑值图标,就是圆圈的x值y值的曲线.控制位置移动的. 选择一个节 ...