|
|
@ -1,20 +1,18 @@
|
|
|
|
export class cookieHandler {
|
|
|
|
export class cookieHandler {
|
|
|
|
constructor(expires) {
|
|
|
|
constructor() {
|
|
|
|
this.expires = expires;
|
|
|
|
this.cookies = {};
|
|
|
|
this.reload();
|
|
|
|
this.reload();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
getCookie(key){
|
|
|
|
getCookies(){
|
|
|
|
if (!this.cookies) return undefined;
|
|
|
|
return this.cookies;
|
|
|
|
let cookie = this.cookies.find(cookie => cookie.split('=')[0] === key);
|
|
|
|
|
|
|
|
return cookie ? cookie.split('=')[1] : false;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
setCookie(object){
|
|
|
|
setCookie(cookies){
|
|
|
|
object.forEach((value, key) => {
|
|
|
|
Object.keys(cookies).forEach(key => {
|
|
|
|
this.cookies[key] = value;
|
|
|
|
this.cookies[key] = cookies[key];
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
parseCookie(string){
|
|
|
|
parseCookie(string){
|
|
|
|
let cookies;
|
|
|
|
let cookies = {};
|
|
|
|
string.replace(/ /g, '').split(';').forEach(cookie => {
|
|
|
|
string.replace(/ /g, '').split(';').forEach(cookie => {
|
|
|
|
let arr = cookie.split('=');
|
|
|
|
let arr = cookie.split('=');
|
|
|
|
cookies[arr[0]] = arr[1];
|
|
|
|
cookies[arr[0]] = arr[1];
|
|
|
@ -23,15 +21,26 @@ export class cookieHandler {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
reload(){
|
|
|
|
reload(){
|
|
|
|
if (document.cookie) this.cookies = this.parseCookie(document.cookie);
|
|
|
|
if (document.cookie) this.cookies = this.parseCookie(document.cookie);
|
|
|
|
|
|
|
|
console.log('cookie loaded')
|
|
|
|
|
|
|
|
console.log(this.cookies);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
store(){
|
|
|
|
store(){
|
|
|
|
document.cookie = this.toString();
|
|
|
|
Object.keys(this.cookies).forEach(key => {
|
|
|
|
|
|
|
|
document.cookie = `${key}=${this.cookies[key]};`;
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
console.log('cookie stored');
|
|
|
|
|
|
|
|
console.log(this.cookies);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
toString(cookies = this.cookies){
|
|
|
|
toString(cookies = this.cookies){
|
|
|
|
let string = '';
|
|
|
|
let string = '';
|
|
|
|
cookies.forEach((value, key) => {
|
|
|
|
Object.keys(cookies).forEach(key => {
|
|
|
|
string += `${key}=${value}; `;
|
|
|
|
string += `${key}=${cookies[key]}; `;
|
|
|
|
})
|
|
|
|
})
|
|
|
|
return string;
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
expires(days){
|
|
|
|
|
|
|
|
this.setCookie({
|
|
|
|
|
|
|
|
expires: new Date(Date.now() + 86400 * 10000 * days)
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|