From a2479875d13f551d711a64804b19120913b3cd1d Mon Sep 17 00:00:00 2001 From: adb Date: Sat, 13 Mar 2021 00:43:18 +0100 Subject: [PATCH] add cookie login --- src/lib/cookieHandler.js | 35 ++++++++++++++++++++++------------- src/main.js | 3 +-- src/views/login.vue | 13 ++++++++++++- 3 files changed, 35 insertions(+), 16 deletions(-) diff --git a/src/lib/cookieHandler.js b/src/lib/cookieHandler.js index e37aaea..88462c4 100644 --- a/src/lib/cookieHandler.js +++ b/src/lib/cookieHandler.js @@ -1,20 +1,18 @@ export class cookieHandler { - constructor(expires) { - this.expires = expires; + constructor() { + this.cookies = {}; this.reload(); } - getCookie(key){ - if (!this.cookies) return undefined; - let cookie = this.cookies.find(cookie => cookie.split('=')[0] === key); - return cookie ? cookie.split('=')[1] : false; + getCookies(){ + return this.cookies; } - setCookie(object){ - object.forEach((value, key) => { - this.cookies[key] = value; + setCookie(cookies){ + Object.keys(cookies).forEach(key => { + this.cookies[key] = cookies[key]; }) } parseCookie(string){ - let cookies; + let cookies = {}; string.replace(/ /g, '').split(';').forEach(cookie => { let arr = cookie.split('='); cookies[arr[0]] = arr[1]; @@ -23,15 +21,26 @@ export class cookieHandler { } reload(){ if (document.cookie) this.cookies = this.parseCookie(document.cookie); + console.log('cookie loaded') + console.log(this.cookies); } 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){ let string = ''; - cookies.forEach((value, key) => { - string += `${key}=${value}; `; + Object.keys(cookies).forEach(key => { + string += `${key}=${cookies[key]}; `; }) return string; } + expires(days){ + this.setCookie({ + expires: new Date(Date.now() + 86400 * 10000 * days) + }); + } } diff --git a/src/main.js b/src/main.js index fe533b2..78856ee 100644 --- a/src/main.js +++ b/src/main.js @@ -10,8 +10,7 @@ Vue.use(VueRouter); export let matrix = new MatrixHandler(); -let cookie = new cookieHandler().getCookie(); -console.log(`cookie => ${cookie}`) +let cookie = new cookieHandler().getCookies(); if (cookie && cookie.baseUrl && cookie.accessToken && cookie.userId) { matrix.tokenLogin(cookie.baseUrl, cookie.accessToken, cookie.userId); } diff --git a/src/views/login.vue b/src/views/login.vue index def6f6a..5fe9a60 100644 --- a/src/views/login.vue +++ b/src/views/login.vue @@ -19,6 +19,7 @@