add cookie login

update_chat
adb 4 years ago
parent 95b8e38dd4
commit a2479875d1

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

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

@ -19,6 +19,7 @@
<script>
import textbtn from '@/components/textbtn';
import {matrix} from '@/main.js';
import {cookieHandler} from "@/lib/cookieHandler";
export default {
name: "login.vue",
@ -42,7 +43,17 @@ export default {
}
matrix.login(this.user, this.password, this.homeServer, (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(){
matrix.logout();

Loading…
Cancel
Save