Login on reload
To get a new access_token if the old one has expired. Should fix issue #36.
This commit is contained in:
parent
5645e32cf0
commit
933de4a2ec
5 changed files with 64 additions and 18 deletions
|
|
@ -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");
|
||||
})
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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(
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
@ -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() {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue