12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- export default class CookieManager {
- constructor() {
- this.expiresDays = 30
- }
- raw() {
- return document.cookie
- }
- parsed() {
- let result = {}
- if (this.raw() == "") {
- return {}
- }
- this.raw().split(";").forEach((part) => {
- const data = part.trim().split("=")
- result[data[0]] = data[1]
- })
- return result
- }
- save(data) {
- if (!data['expires']) {
- const d = new Date();
- d.setTime(d.getTime() + (this.expiresDays * 24 * 60 * 60 * 1000));
- data['expires'] = d.toUTCString();
- }
- if (!data['path']) {
- data['path'] = "/"
- }
- let cookieData = []
- for (let index in data) {
- cookieData.push(`${index}=${data[index]}`)
- }
- cookieData.push(`SameSite=Strict`)
- cookieData.push(`Secure`)
- document.cookie = cookieData.join("; ")
- }
- get(key) {
- let cookieData = this.parsed()
- if(Object.keys(cookieData).length == 0) {
- return null;
- }
- return cookieData[key]
- }
- add(key, value) {
- let data = this.parsed()
- data[key] = value
- this.save(data)
- }
- parseJwt(token) {
- if(!token) {
- return null
- }
- return JSON.parse(decodeURIComponent(atob(token.split('.')[1].replace(/-/g, '+').replace(/_/g, '/')).split('').map(function(c) {
- return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2)
- }).join('')))
- };
- }
|