From 933de4a2ec57ccee1dd5e94867843f4b57ae783f Mon Sep 17 00:00:00 2001 From: N-Pex Date: Wed, 27 Jan 2021 12:57:23 +0100 Subject: [PATCH] Login on reload To get a new access_token if the old one has expired. Should fix issue #36. --- src/App.vue | 4 ++-- src/components/Join.vue | 4 ++-- src/components/Login.vue | 6 +----- src/models/user.js | 29 ++++++++++++++++++++++--- src/services/matrix.service.js | 39 ++++++++++++++++++++++++++++------ 5 files changed, 64 insertions(+), 18 deletions(-) diff --git a/src/App.vue b/src/App.vue index 36497c7..977910b 100644 --- a/src/App.vue +++ b/src/App.vue @@ -84,9 +84,9 @@ export default { currentUser: { immediate: true, handler(ignorednewVal, ignoredoldVal) { - if (this.loggedIn()) { + if (this.currentUser) { this.$matrix - .getMatrixClient(this.currentUser) + .login(this.currentUser) .then(() => { console.log("Matrix client ready"); }) diff --git a/src/components/Join.vue b/src/components/Join.vue index 6a2ce49..869b1c8 100644 --- a/src/components/Join.vue +++ b/src/components/Join.vue @@ -157,7 +157,7 @@ export default { this.waitingForMembership = true; const self = this; this.$matrix - .getMatrixClient(this.currentUser) + .login(this.currentUser) .then(() => { self.$matrix.setCurrentRoomId(self.roomId); // Go to this room, now or when joined. @@ -246,7 +246,7 @@ export default { this.loadingMessage = "Logging in..."; var clientPromise; if (this.currentUser) { - clientPromise = this.$matrix.getMatrixClient(this.currentUser); + clientPromise = this.$matrix.login(this.currentUser); } else { clientPromise = this.$store.dispatch("auth/login", this.guestUser); } diff --git a/src/components/Login.vue b/src/components/Login.vue index 40b9ba8..d7b8178 100644 --- a/src/components/Login.vue +++ b/src/components/Login.vue @@ -115,11 +115,7 @@ export default { // Is it a full matrix user id? Modify a copy, so that the UI will still show the full ID. var user = Object.assign({}, this.user); - if (user.username.startsWith('@') && user.username.includes(':')) { - const parts = user.username.split(":"); - user.username = parts[0].substring(1); - user.server = "https://" + parts[1]; - } + user.normalize(); this.loading = true; this.$store.dispatch("auth/login", user).then( diff --git a/src/models/user.js b/src/models/user.js index 5e3f899..125ac9c 100644 --- a/src/models/user.js +++ b/src/models/user.js @@ -1,8 +1,31 @@ export default class User { - constructor(server, username, password, is_guest) { - this.server = server; - this.username = username; + constructor(home_server, user_id, password, is_guest) { + this.home_server = home_server; + this.user_id = user_id; this.password = password; this.is_guest = is_guest || false } + + normalize = function() { + if (this.user_id.startsWith('@') && this.user_id.includes(':')) { + const parts = this.user_id.split(":"); + this.user_id = parts[0].substring(1); + this.home_server = "https://" + parts[1]; + } + }; + + static homeServerUrl = function(home_server) { + if (home_server && !home_server.startsWith("https://")) { + return "https://" + home_server; + } + return home_server; + }; + + static localPart(user_id) { + if (user_id && user_id.startsWith('@') && user_id.includes(':')) { + const parts = user_id.split(":"); + return parts[0].substring(1); + } + return user_id; + } } \ No newline at end of file diff --git a/src/services/matrix.service.js b/src/services/matrix.service.js index 0c090cf..c165364 100644 --- a/src/services/matrix.service.js +++ b/src/services/matrix.service.js @@ -1,6 +1,7 @@ global.Olm = require("olm"); import sdk from "matrix-js-sdk"; import util from "../plugins/utils"; +import User from "../models/user"; const LocalStorageCryptoStore = require("matrix-js-sdk/lib/crypto/store/localStorage-crypto-store") .LocalStorageCryptoStore; @@ -61,10 +62,10 @@ export default { methods: { login(user) { - const tempMatrixClient = sdk.createClient(user.server); + const tempMatrixClient = sdk.createClient(User.homeServerUrl(user.home_server)); var promiseLogin; - if (user.is_guest) { + if (user.is_guest && !user.access_token) { // Generate random username and password. We don't user REAL matrix // guest accounts because 1. They are not allowed to post media, 2. They // can not use avatars and 3. They can not seamlessly be upgraded to real accounts. @@ -83,11 +84,23 @@ export default { localStorage.setItem('user', JSON.stringify(response)); return response; }) + } else if (!user.is_guest && user.access_token) { + // Logged in on "real" account + promiseLogin = Promise.resolve("Already logged in"); } else { + var data = { user: User.localPart(user.user_id), password: user.password, type: "m.login.password" }; + if (user.device_id) { + data.device_id = user.device_id; + } promiseLogin = tempMatrixClient - .login("m.login.password", { user: user.username, password: user.password, type: "m.login.password" }) + .login("m.login.password", data) .then((response) => { - localStorage.setItem('user', JSON.stringify(response)); + var u = response; + if (user.is_guest) { + // Copy over needed properties + u = Object.assign(user, response); + } + localStorage.setItem('user', JSON.stringify(u)); return response; }) } @@ -104,8 +117,8 @@ export default { this.matrixClient.stopClient(); this.matrixClient = null; this.matrixClientReady = false; - localStorage.removeItem('user'); } + localStorage.removeItem('user'); this.$store.commit("setCurrentRoomId", null); }, @@ -259,7 +272,21 @@ export default { onSessionLoggedOut() { console.log("Logged out!"); - this.logout(); + if (this.matrixClient) { + this.removeMatrixClientListeners(this.matrixClient); + this.matrixClient.stopClient(); + this.matrixClient = null; + this.matrixClientReady = false; + } + this.$store.commit("setCurrentRoomId", null); + + // Clear the access token + var user = JSON.parse(localStorage.getItem('user')); + if (user) { + delete user.access_token; + } + localStorage.setItem('user', JSON.stringify(user)); + this.$navigation.push({ name: "Login" }, -1); }, reloadRooms() {