add cookie login

update_chat
adb 4 years ago
parent 95b8e38dd4
commit a2479875d1

@ -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)
});
}
} }

@ -10,8 +10,7 @@ Vue.use(VueRouter);
export let matrix = new MatrixHandler(); export let matrix = new MatrixHandler();
let cookie = new cookieHandler().getCookie(); let cookie = new cookieHandler().getCookies();
console.log(`cookie => ${cookie}`)
if (cookie && cookie.baseUrl && cookie.accessToken && cookie.userId) { if (cookie && cookie.baseUrl && cookie.accessToken && cookie.userId) {
matrix.tokenLogin(cookie.baseUrl, cookie.accessToken, cookie.userId); matrix.tokenLogin(cookie.baseUrl, cookie.accessToken, cookie.userId);
} }

@ -19,6 +19,7 @@
<script> <script>
import textbtn from '@/components/textbtn'; import textbtn from '@/components/textbtn';
import {matrix} from '@/main.js'; import {matrix} from '@/main.js';
import {cookieHandler} from "@/lib/cookieHandler";
export default { export default {
name: "login.vue", name: "login.vue",
@ -42,7 +43,17 @@ export default {
} }
matrix.login(this.user, this.password, this.homeServer, (error) => { matrix.login(this.user, this.password, this.homeServer, (error) => {
this.loginError = `login failed: ${error}`; this.loginError = `login failed: ${error}`;
}, ()=> this.$router.push('/rooms/')); }, (token)=> {
let cookie = new cookieHandler();
cookie.setCookie({
baseUrl: this.homeServer,
userId: this.user,
accessToken: token
});
cookie.expires(15);
cookie.store();
this.$router.push('/rooms/');
});
}, },
logout(){ logout(){
matrix.logout(); matrix.logout();

Loading…
Cancel
Save