diff --git a/src/matrix/Client.js b/src/matrix/Client.js index 0eb9f6e0..7423b431 100644 --- a/src/matrix/Client.js +++ b/src/matrix/Client.js @@ -337,6 +337,19 @@ export class Client { } } + /** + * Update the access token in use by the client. + * Will also update the token in session storage. + * @param {string} token A Matrix Access Token + */ + async updateAccessToken(token) { + if (!this._session) { + throw Error("No session loaded, cannot update access token"); + } + this._session.updateAccessToken(token); + await this._platform.sessionInfoStorage.updateAccessToken(this._sessionId, token); + } + async _waitForFirstSync() { this._sync.start(); this._status.set(LoadStatus.FirstSync); diff --git a/src/matrix/Session.js b/src/matrix/Session.js index 3fc3b006..348b626b 100644 --- a/src/matrix/Session.js +++ b/src/matrix/Session.js @@ -1094,6 +1094,15 @@ export class Session { return body.room_id; }); } + + /** + * Updates the access token used by the API. Does NOT + * change the token in storage. + * @param {string} token + */ + updateAccessToken(token) { + this._hsApi.updateAccessToken(token); + } } import {FeatureSet} from "../features"; diff --git a/src/matrix/net/HomeServerApi.ts b/src/matrix/net/HomeServerApi.ts index eebc692a..50e51c21 100644 --- a/src/matrix/net/HomeServerApi.ts +++ b/src/matrix/net/HomeServerApi.ts @@ -46,7 +46,7 @@ type BaseRequestOptions = { export class HomeServerApi { private readonly _homeserver: string; - private readonly _accessToken: string; + private _accessToken: string; private readonly _requestFn: RequestFunction; private readonly _reconnector: Reconnector; @@ -125,6 +125,14 @@ export class HomeServerApi { return this._authedRequest("GET", this._url(csPath, options?.prefix || CS_R0_PREFIX), queryParams, body, options); } + /** + * Update the access token used by the API. + * @param token + */ + public updateAccessToken(token: string) { + this._accessToken = token; + } + sync(since: string, filter: string, timeout: number, options?: BaseRequestOptions): IHomeServerRequest { return this._get("/sync", {since, timeout, filter}, undefined, options); } diff --git a/src/matrix/sessioninfo/localstorage/SessionInfoStorage.ts b/src/matrix/sessioninfo/localstorage/SessionInfoStorage.ts index d6596a61..795333bc 100644 --- a/src/matrix/sessioninfo/localstorage/SessionInfoStorage.ts +++ b/src/matrix/sessioninfo/localstorage/SessionInfoStorage.ts @@ -37,6 +37,7 @@ interface ISessionInfo { interface ISessionInfoStorage { getAll(): Promise; updateLastUsed(id: string, timestamp: number): Promise; + updateAccessToken(id: string, token: string): Promise; get(id: string): Promise; add(sessionInfo: ISessionInfo): Promise; delete(sessionId: string): Promise; @@ -60,14 +61,22 @@ export class SessionInfoStorage implements ISessionInfoStorage { return Promise.resolve([]); } + async updateAccessToken(id: string, accessToken: string): Promise { + const sessions = await this.getAll(); + const session = sessions.find(session => session.id === id); + if (!session) { + throw Error('No session found'); + } + session.accessToken = accessToken; + localStorage.setItem(this._name, JSON.stringify(sessions)); + } + async updateLastUsed(id: string, timestamp: number): Promise { const sessions = await this.getAll(); - if (sessions) { - const session = sessions.find(session => session.id === id); - if (session) { - session.lastUsed = timestamp; - localStorage.setItem(this._name, JSON.stringify(sessions)); - } + const session = sessions.find(session => session.id === id); + if (session) { + session.lastUsed = timestamp; + localStorage.setItem(this._name, JSON.stringify(sessions)); } }