matrix-org / gomatrix

A Golang Matrix client
Apache License 2.0
285 stars 53 forks source link

Would it not be easier if Login would return a valid client? #72

Closed danielniccoli closed 4 years ago

danielniccoli commented 4 years ago

I'm still very new to Go and gomatrix, so please excuse my question if it sounds silly.

If I want to login, why do I need to instantiate NewClient with empty userID and accessToken and then proceed to SetCredentials?

Login is the first thing you must do (if you not already know the token). Why not have Login return a client with userid and access_token already set?

Something like:

func ExampleClient_Login() {
    cli, _ := PasswordLogin("http://localhost:8008", "alice", "wonderland")
}

Instead of:

// Login to a local homeserver and set the user ID and access token on success.
func ExampleClient_Login() {
    cli, _ := NewClient("http://localhost:8008", "", "")
    resp, err := cli.Login(&ReqLogin{
        Type:     "m.login.password",
        User:     "alice",
        Password: "wonderland",
    })
    if err != nil {
        panic(err)
    }
    cli.SetCredentials(resp.UserID, resp.AccessToken)
}
kegsay commented 4 years ago

Primarily because login is not guaranteed to be single step - 2FA would make cli, _ := PasswordLogin("http://localhost:8008", "alice", "wonderland") not work as you'd need to have code in the middle to handle additional authentication. We don't know at compile time which servers will require additional steps, so the API has to be contorted to handle that. It's also just more consistent request/response pairings since currently this library is mainly an HTTP wrapper.