From ce601628271c65e1a007dfcaff49933adba16047 Mon Sep 17 00:00:00 2001 From: Richard van der Hoff <1389908+richvdh@users.noreply.github.com> Date: Mon, 20 Jan 2025 21:18:51 +0000 Subject: [PATCH] Deprecate `MatrixClient.login` and replace with `loginRequest` (#4632) `MatrixClient.login` has some very unintuitive behaviour where it stashes the access token, but not the device id, refresh token, etc etc, which led people to imagine that they had a functional `MatrixClient` when they didn't. In practice, you have to create a *new* `MatrixClient` given the `LoginResponse`. As the first step for sorting this out, this deprecates the broken method and replaces it with one that has sensible behaviour. --- src/client.ts | 52 +++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 38 insertions(+), 14 deletions(-) diff --git a/src/client.ts b/src/client.ts index 0e49b16091..d11350767f 100644 --- a/src/client.ts +++ b/src/client.ts @@ -8246,27 +8246,33 @@ export class MatrixClient extends TypedEventEmitter): Promise { - return this.http - .authedRequest(Method.Post, "/login", undefined, { - ...data, - type: loginType, - }) - .then((response) => { - if (response.access_token && response.user_id) { - this.http.opts.accessToken = response.access_token; - this.credentials = { - userId: response.user_id, - }; - } - return response; - }); + return this.loginRequest({ + ...data, + type: loginType, + }).then((response) => { + if (response.access_token && response.user_id) { + this.http.opts.accessToken = response.access_token; + this.credentials = { + userId: response.user_id, + }; + } + return response; + }); } /** * @returns Promise which resolves to a LoginResponse object * @returns Rejects: with an error response. + * + * @deprecated This method has unintuitive behaviour: it updates the `MatrixClient` instance with *some* of the + * returned credentials. Instead, call {@link loginRequest} with `data.type: "m.login.password"`, and create a new + * `MatrixClient` instance using the results. See https://github.com/matrix-org/matrix-js-sdk/issues/4502. */ public loginWithPassword(user: string, password: string): Promise { return this.login("m.login.password", { @@ -8311,6 +8317,10 @@ export class MatrixClient extends TypedEventEmitter { return this.login("m.login.token", { @@ -8318,6 +8328,20 @@ export class MatrixClient extends TypedEventEmitter { + return await this.http.authedRequest(Method.Post, "/login", undefined, data); + } + /** * Logs out the current session. * Obviously, further calls that require authorisation should fail after this