From 4821e08b80d6884391667526af3bc3a6bd3b81a9 Mon Sep 17 00:00:00 2001 From: adb Date: Thu, 11 Mar 2021 23:39:26 +0100 Subject: [PATCH] update login --- src/lib/cookieHandler.js | 3 ++- src/lib/matrixHandler.js | 28 +++++++++++++++++++++------- src/main.js | 23 ++++++++++++----------- src/matrix.js | 28 +++++++++------------------- src/views/login.vue | 40 ++++++++++++++++++++++++++++++---------- 5 files changed, 74 insertions(+), 48 deletions(-) diff --git a/src/lib/cookieHandler.js b/src/lib/cookieHandler.js index 62fa832..e37aaea 100644 --- a/src/lib/cookieHandler.js +++ b/src/lib/cookieHandler.js @@ -4,6 +4,7 @@ export class cookieHandler { 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; } @@ -21,7 +22,7 @@ export class cookieHandler { return cookies; } reload(){ - this.cookies = this.parseCookie(document.cookie) + if (document.cookie) this.cookies = this.parseCookie(document.cookie); } store(){ document.cookie = this.toString(); diff --git a/src/lib/matrixHandler.js b/src/lib/matrixHandler.js index f7f5c45..a435fe1 100644 --- a/src/lib/matrixHandler.js +++ b/src/lib/matrixHandler.js @@ -1,16 +1,15 @@ import matrix from 'matrix-js-sdk'; -import {cookieHandler} from '@/lib/cookieHandler.js'; + +export let client = undefined; export class MatrixHandler { constructor(clientDisplayName = 'matrix-chat') { this.clientDisplayName = clientDisplayName; this.accessToken; - let cookie = new cookieHandler().getCookie(); this.client = undefined; - if (cookie.baseUrl && cookie.accessToken && cookie.userId) - this.tokenLogin(cookie.baseUrl, cookie.accessToken, cookie.userId); + this.rooms = undefined; } - login(user, password, baseUrl, onError){ + login(user, password, baseUrl, onError, callback = ()=>{}){ if (this.client){ console.log('there is already an active session'); return; } this.client = new matrix.createClient({ baseUrl: baseUrl @@ -20,19 +19,34 @@ export class MatrixHandler { password: password, initial_device_display_name: this.clientDisplayName, }).then((response) => { - console.log(`access token => ${response.access_token}`); if (response.error) { + this.logout(); console.log(`login error => ${response.error}`); onError(response.error); } + if (response.access_token){ + console.log(`access token => ${response.access_token}`); + console.log(this.client.getRooms()); + callback(response.access_token); + } + }).catch(error => { + this.logout(); + console.log(error); + onError(error.toString()); }) } tokenLogin(baseUrl, accessToken, userId){ if (this.client){ console.log('there is already an active session'); return; } - this.client = new matrix.createClient({baseUrl, accessToken, userId}); + this.client = new matrix.createClient({baseUrl, accessToken, userId}).then(response => { + console.log(response); + }); this.client.startClient(); this.client.once('sync', (state) => { console.log(state); }); } + logout(){ + this.client.stopClient(); + this.client = undefined; + } } \ No newline at end of file diff --git a/src/main.js b/src/main.js index 7ae9ae9..fe533b2 100644 --- a/src/main.js +++ b/src/main.js @@ -2,16 +2,18 @@ import Vue from 'vue' import VueRouter from 'vue-router' import App from './App.vue' import {router} from './router.js' +import {MatrixHandler} from './lib/matrixHandler.js' +import {cookieHandler} from './lib/cookieHandler.js'; -Vue.config.productionTip = false -Vue.use(VueRouter) +Vue.config.productionTip = false; +Vue.use(VueRouter); +export let matrix = new MatrixHandler(); - -export default { - methods: { - router(route){router.push(route)} - } +let cookie = new cookieHandler().getCookie(); +console.log(`cookie => ${cookie}`) +if (cookie && cookie.baseUrl && cookie.accessToken && cookie.userId) { + matrix.tokenLogin(cookie.baseUrl, cookie.accessToken, cookie.userId); } new Vue({ @@ -19,8 +21,7 @@ new Vue({ router, template: '', components: {App}, - data(){ - return { - } + data() { + return {} } -}).$mount('#app') +}).$mount('#app'); diff --git a/src/matrix.js b/src/matrix.js index f4819df..633cd3c 100644 --- a/src/matrix.js +++ b/src/matrix.js @@ -1,8 +1,8 @@ -import matrix from 'matrix-js-sdk'; +/*import matrix from 'matrix-js-sdk'; import main from '@/main.js'; // import Vue from 'vue'; -let client = matrix.createClient({}); +let client = undefined; let session = { user: '', baseUrl: '', @@ -44,23 +44,12 @@ export default { data() { return { session, + client }; }, methods: { login() { - if (session.accessToken !== '') { - main.methods.error('you are already logged in'); - return; - } if (session.login.user === '') { - main.methods.error('username is empty'); - return; - } if (session.login.password === '') { - main.methods.error('password is empty'); - return; - } if (!(session.login.user.includes('@') && session.login.user.includes(':'))) { - main.methods.error('username is in wrong style'); - return; - } + client = matrix.createClient({ baseUrl: session.login.baseUrl }); @@ -72,6 +61,7 @@ export default { document.cookie = `accessToken=${response.access_token}`; document.cookie = `userId=${session.login.user}`; document.cookie = `baseUrl=${session.login.baseUrl}`; + document.cookie = `SameSite=Strict`; document.cookie = `expires=${new Date(Date.now() + 86400 * 10 * 1000)}`; session = { user: session.login.user, @@ -86,12 +76,12 @@ export default { console.log(`login error => ${response.error}`); } window.location.href = '/#/rooms/'; - window.location.reload(); + window.location.reload();*/ /*client.startClient(); client.once('sync', (state) => { console.log(state); });*/ - }); +/* }); }, logout(){ document.cookie = `accessToken=`; @@ -124,7 +114,7 @@ function getCookie(key) { return cookie ? cookie.split('=')[1] : false; } -client.on('event', (event) => { +/*client.on('event', (event) => { //console.log(event.getType()); //console.log(event); if (event.getType() === 'm.room.name') { @@ -175,4 +165,4 @@ client.on('Room.timeline', (event, room) => { } else document.getElementById('scrollDown').style.display = 'block'; } } -}); +});*/ diff --git a/src/views/login.vue b/src/views/login.vue index ecdc93f..def6f6a 100644 --- a/src/views/login.vue +++ b/src/views/login.vue @@ -1,15 +1,16 @@