matrix-org / matrix-org/matrix-js-sdk

How to create a `MatrixClient` given a `loginResponse`

Open
#4,633 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-Documentation T-Task
Dominant language
TypeScript
Stars
2.2k
Forks
704
Avg merge
1d 20h
Merged PRs (30d)
40

Description

Previously, creating a MatrixClient for a newly-logged-in user looked something like this:

const hs = "https://matrix.org/";
const loginClient = sdk.createClient({ baseUrl: hs });
const loginResponse = await client.loginRequest({
    type: "m.login.password",
    identifier: { type: "m.id.user", user: "userid" },
    password: "mysecretpassword",
});

// Create a new matrix client
const matrixClient = sdk.createClient({
    baseUrl: loginResponse.well_known?.["m.homeserver"] ?? hs,
    accessToken: loginResponse.access_token,
    userId: loginResponse.user_id,
    deviceId: loginResponse.device_id,
});

Even that is subtle enough, and totally undocumented. However, I believe that, since MSC2918 support was added, it is no longer adequate, and I don't know what the right way to do it is. Is the tokenRefreshFunction required? If so, what do I set it to?

Worse, what little documentation there is (in the README) is wrong, since it omits things not only the refresh token, but things like deviceId.

This could really do with updating. We should probably have a utility function to make it easy.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the README's client-creation documentation and the sdk.createClient/loginRequest entry points shown in the issue. Determine the supported construction from loginResponse, including token refresh behavior and deviceId, then update the documentation so a newcomer can create a working client without guesswork.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
api, authentication, documentation
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.