Fix "rememberMe" functionality

This commit is contained in:
N-Pex 2025-05-16 09:55:15 +02:00
parent ac4af0a53d
commit aa1ce4ee48
9 changed files with 89 additions and 97 deletions

View file

@ -4,6 +4,7 @@ import { TimelineWindow, EventTimeline, EventStatus } from "matrix-js-sdk";
import util, { STATE_EVENT_ROOM_DELETED, STATE_EVENT_ROOM_TYPE, ROOM_TYPE_CHANNEL, ROOM_TYPE_FILE_MODE, ROOM_TYPE_VOICE_MODE, ROOM_TYPE_DEFAULT } from "../plugins/utils";
import User from "../models/user";
import * as LocalStorageCryptoStoreClass from "matrix-js-sdk/lib/crypto/store/localStorage-crypto-store";
import rememberMeMixin from "../components/rememberMeMixin";
const LocalStorageCryptoStore = LocalStorageCryptoStoreClass.LocalStorageCryptoStore;
@ -34,6 +35,7 @@ export default {
// return ret;
// });
const matrixService = createApp({
mixins: [ rememberMeMixin ],
data() {
return {
matrixClient: null,
@ -43,6 +45,7 @@ export default {
userAvatar: null,
currentRoom: null,
notificationCount: 0,
legacyCryptoStore: undefined,
};
},
@ -122,8 +125,11 @@ export default {
methods: {
createCryptoStore() {
console.log("create crypto store");
return new LocalStorageCryptoStore(this.$store.getters.storage);
if (!this.legacyCryptoStore) {
console.log("create crypto store");
this.legacyCryptoStore = new LocalStorageCryptoStore(this.$store.getters.storage);
}
return this.legacyCryptoStore;
},
login(user, registrationFlowHandler, createUser = false) {
return util.getMatrixBaseUrl(user, this.$config).then((baseUrl) => {
@ -264,14 +270,12 @@ export default {
});
},
getMatrixClient(user) {
async getMatrixClient(user) {
if (user === undefined) {
user = this.$store.state.auth.user;
}
if (this.matrixClientReady) {
return new Promise((resolve, ignoredreject) => {
resolve(user);
});
return user;
} else if (this.matrixClient) {
return new Promise((resolve, ignoredreject) => {
this.matrixClient.once("Matrix.initialized", (ignoredclient) => {
@ -281,62 +285,62 @@ export default {
}
const matrixStore = new sdk.MemoryStore(this.$store.getters.storage);
const cryptoStore = this.createCryptoStore();
return util.getMatrixBaseUrl(user, this.$config).then((baseUrl) => {
var opts = {
baseUrl: baseUrl,
userId: user.user_id,
store: matrixStore,
deviceId: user.device_id,
accessToken: user.access_token,
timelineSupport: true,
unstableClientRelationAggregation: true,
cryptoStore: this.createCryptoStore()
//useAuthorizationHeader: true
};
this.matrixClient = sdk.createClient(opts);
// if (user.is_guest) {
// this.matrixClient.setGuest(true);
// }
console.error("Created client", this.matrixClient);
return this.matrixClient
.initRustCrypto()
.then(() => {
console.log("Crypto initialized");
this.addMatrixClientListeners(this.matrixClient);
this.matrixClient.startClient();
return this.matrixClient;
})
.then((matrixClient) => {
if (matrixClient.isInitialSyncComplete()) {
console.log("Initial sync done already!");
return matrixClient;
} else {
return new Promise((resolve, reject) => {
matrixClient.once("sync", function (state, ignoredprevState, ignoredres) {
console.log(state); // state will be 'PREPARED' when the client is ready to use
if (state == "PREPARED") {
resolve(matrixClient);
} else if (state == "ERROR") {
reject("Error syncing");
}
});
});
if (cryptoStore) {
const migrationState = await cryptoStore.getMigrationState();
if (migrationState >= 0) {
const store = this.$store.getters.storage;
// Rust crypto migration is broken, needs the sessionId to be set. So do that here, if needed.
for (let i = 0; i < store.length; ++i) {
const key = store.key(i);
if (key.startsWith("crypto.sessions/")) {
const sessions = JSON.parse(store.getItem(key));
for (let sessionId of Object.keys(sessions)) {
sessions[sessionId].sessionId = sessionId;
sessions[sessionId].deviceKey = key.substring("crypto.sessions/".length);
}
store.setItem(key, JSON.stringify(sessions));
}
})
.then(() => {
return this.matrixClient.isVersionSupported("v1.11");
})
.then((authedMediaSupported) => {
this.useAuthedMedia = authedMediaSupported;
}
}
}
// Ready to use! Start by loading rooms.
this.initClient();
return user;
const baseUrl = await util.getMatrixBaseUrl(user, this.$config);
var opts = {
baseUrl: baseUrl,
userId: user.user_id,
store: matrixStore,
deviceId: user.device_id,
accessToken: user.access_token,
timelineSupport: true,
unstableClientRelationAggregation: true,
cryptoStore: cryptoStore
//useAuthorizationHeader: true
};
const matrixClient = sdk.createClient(opts);
this.matrixClient = matrixClient;
await this.matrixClient.initRustCrypto({useIndexedDB: this.rememberMe});// storageKey: cryptoStorageKey});
console.log("Crypto initialized");
this.addMatrixClientListeners(this.matrixClient);
this.matrixClient.startClient();
if (!this.matrixClient.isInitialSyncComplete()) {
await new Promise((resolve, reject) => {
this.matrixClient.once("sync", function (state, ignoredprevState, ignoredres) {
console.log(state); // state will be 'PREPARED' when the client is ready to use
if (state == "PREPARED") {
resolve(matrixClient);
} else if (state == "ERROR") {
reject("Error syncing");
}
});
});
});
}
this.useAuthedMedia = await this.matrixClient.isVersionSupported("v1.11");
// Ready to use! Start by loading rooms.
this.initClient();
return user;
},
/**