Skip to content

Commit ce60162

Browse files
authored
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.
1 parent b45d51a commit ce60162

File tree

1 file changed

+38
-14
lines changed

1 file changed

+38
-14
lines changed

src/client.ts

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8246,27 +8246,33 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
82468246
/**
82478247
* @returns Promise which resolves to a LoginResponse object
82488248
* @returns Rejects: with an error response.
8249+
*
8250+
* @deprecated This method has unintuitive behaviour: it updates the `MatrixClient` instance with *some* of the
8251+
* returned credentials. Instead, call {@link loginRequest} and create a new `MatrixClient` instance using the
8252+
* results. See https://github.com/matrix-org/matrix-js-sdk/issues/4502.
82498253
*/
82508254
public login(loginType: LoginRequest["type"], data: Omit<LoginRequest, "type">): Promise<LoginResponse> {
8251-
return this.http
8252-
.authedRequest<LoginResponse>(Method.Post, "/login", undefined, {
8253-
...data,
8254-
type: loginType,
8255-
})
8256-
.then((response) => {
8257-
if (response.access_token && response.user_id) {
8258-
this.http.opts.accessToken = response.access_token;
8259-
this.credentials = {
8260-
userId: response.user_id,
8261-
};
8262-
}
8263-
return response;
8264-
});
8255+
return this.loginRequest({
8256+
...data,
8257+
type: loginType,
8258+
}).then((response) => {
8259+
if (response.access_token && response.user_id) {
8260+
this.http.opts.accessToken = response.access_token;
8261+
this.credentials = {
8262+
userId: response.user_id,
8263+
};
8264+
}
8265+
return response;
8266+
});
82658267
}
82668268

82678269
/**
82688270
* @returns Promise which resolves to a LoginResponse object
82698271
* @returns Rejects: with an error response.
8272+
*
8273+
* @deprecated This method has unintuitive behaviour: it updates the `MatrixClient` instance with *some* of the
8274+
* returned credentials. Instead, call {@link loginRequest} with `data.type: "m.login.password"`, and create a new
8275+
* `MatrixClient` instance using the results. See https://github.com/matrix-org/matrix-js-sdk/issues/4502.
82708276
*/
82718277
public loginWithPassword(user: string, password: string): Promise<LoginResponse> {
82728278
return this.login("m.login.password", {
@@ -8311,13 +8317,31 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
83118317
* @param token - Login token previously received from homeserver
83128318
* @returns Promise which resolves to a LoginResponse object
83138319
* @returns Rejects: with an error response.
8320+
*
8321+
* @deprecated This method has unintuitive behaviour: it updates the `MatrixClient` instance with *some* of the
8322+
* returned credentials. Instead, call {@link loginRequest} with `data.type: "m.login.token"`, and create a new
8323+
* `MatrixClient` instance using the results. See https://github.com/matrix-org/matrix-js-sdk/issues/4502.
83148324
*/
83158325
public loginWithToken(token: string): Promise<LoginResponse> {
83168326
return this.login("m.login.token", {
83178327
token: token,
83188328
});
83198329
}
83208330

8331+
/**
8332+
* Sends a `POST /login` request to the server.
8333+
*
8334+
* If successful, this will create a new device and access token for the user.
8335+
*
8336+
* @see {@link MatrixClient.loginFlows} which makes a `GET /login` request.
8337+
* @see https://spec.matrix.org/v1.13/client-server-api/#post_matrixclientv3login
8338+
*
8339+
* @param data - Credentials and other details for the login request.
8340+
*/
8341+
public async loginRequest(data: LoginRequest): Promise<LoginResponse> {
8342+
return await this.http.authedRequest<LoginResponse>(Method.Post, "/login", undefined, data);
8343+
}
8344+
83218345
/**
83228346
* Logs out the current session.
83238347
* Obviously, further calls that require authorisation should fail after this

0 commit comments

Comments
 (0)