Skip to content

MatrixClient.login no longer stashes the returned access token #4627

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 17 additions & 20 deletions src/client.ts
Original file line number Diff line number Diff line change
@@ -8244,29 +8244,23 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
}

/**
* @returns Promise which resolves to a LoginResponse object
* @returns Rejects: with an error response.
* Sends a `/login` request to the server, to create a new device.
*
* Note that the results are *not* persisted in this {@link MatrixClient} object: a new `MatrixClient`
* must be constructed to use the returned details.
*/
public login(loginType: LoginRequest["type"], data: Omit<LoginRequest, "type">): Promise<LoginResponse> {
return this.http
.authedRequest<LoginResponse>(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.http.authedRequest<LoginResponse>(Method.Post, "/login", undefined, {
...data,
type: loginType,
});
}

/**
* @returns Promise which resolves to a LoginResponse object
* @returns Rejects: with an error response.
* Sends a `/login` request to the server with username and password, to create a new device.
*
* Note that the results are *not* persisted in this {@link MatrixClient} object: a new `MatrixClient`
* must be constructed to use the returned details.
*/
public loginWithPassword(user: string, password: string): Promise<LoginResponse> {
return this.login("m.login.password", {
@@ -8308,9 +8302,12 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
}

/**
* Sends a `/login` request to the server with login token, to create a new device.
*
* Note that the results are *not* persisted in this {@link MatrixClient} object: a new `MatrixClient`
* must be constructed to use the returned details.
*
* @param token - Login token previously received from homeserver
* @returns Promise which resolves to a LoginResponse object
* @returns Rejects: with an error response.
*/
public loginWithToken(token: string): Promise<LoginResponse> {
return this.login("m.login.token", {

Unchanged files with check annotations Beta

const resp = await prom;
expect(resp.access_token).toBe(token);
expect(resp.user_id).toBe(userId);
expect(client.getUserId()).toBe(userId);

Check failure on line 1488 in spec/integ/matrix-client-methods.spec.ts

GitHub Actions / Jest [integ] (Node 22)

MatrixClient › login › should persist values to the client opts

expect(received).toBe(expected) // Object.is equality Expected: "@m:t" Received: "@alice:localhost" at Object.toBe (spec/integ/matrix-client-methods.spec.ts:1488:40) at asyncGeneratorStep (node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:17) at _next (node_modules/@babel/runtime/helpers/asyncToGenerator.js:17:9)

Check failure on line 1488 in spec/integ/matrix-client-methods.spec.ts

GitHub Actions / Jest [integ] (Node lts/*)

MatrixClient › login › should persist values to the client opts

expect(received).toBe(expected) // Object.is equality Expected: "@m:t" Received: "@alice:localhost" at Object.toBe (spec/integ/matrix-client-methods.spec.ts:1488:40) at asyncGeneratorStep (node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:17) at _next (node_modules/@babel/runtime/helpers/asyncToGenerator.js:17:9)
expect(client.http.opts.accessToken).toBe(token);
});
});
client.httpBackend.flush("/login", 1, 100);
await client.client.login("m.login.any", { user: "test", password: "12312za" });
expect(client.client.getAccessToken()).toBe(response.access_token);

Check failure on line 31 in spec/unit/login.spec.ts

GitHub Actions / Jest [unit] (Node 22)

Login request › should store "access_token" and "user_id" if in response

expect(received).toBe(expected) // Object.is equality Expected: "194758a9139" Received: null at Object.toBe (spec/unit/login.spec.ts:31:48) at asyncGeneratorStep (node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:17) at _next (node_modules/@babel/runtime/helpers/asyncToGenerator.js:17:9)

Check failure on line 31 in spec/unit/login.spec.ts

GitHub Actions / Jest [unit] (Node lts/*)

Login request › should store "access_token" and "user_id" if in response

expect(received).toBe(expected) // Object.is equality Expected: "194758a9a0d" Received: null at Object.toBe (spec/unit/login.spec.ts:31:48) at asyncGeneratorStep (node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:17) at _next (node_modules/@babel/runtime/helpers/asyncToGenerator.js:17:9)
expect(client.client.getUserId()).toBe(response.user_id);
});
});