Add Login()

With example.
This commit is contained in:
Kegan Dougal 2016-12-09 15:03:19 +00:00
parent 76d9ae9611
commit 9c7c0986d7
4 changed files with 46 additions and 1 deletions

View File

@ -343,6 +343,18 @@ func (cli *Client) RegisterDummy(req *ReqRegister, setOnClient bool) (*RespRegis
return res, nil
}
// Login a user to the homeserver according to http://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-login
// If 'setOnClient' is true, the user ID and access token on login will be set to this client instance.
func (cli *Client) Login(req *ReqLogin, setOnClient bool) (resp *RespLogin, err error) {
urlPath := cli.BuildURL("login")
_, err = cli.MakeRequest("POST", urlPath, req, &resp)
if setOnClient && resp != nil {
cli.UserID = resp.UserID
cli.AccessToken = resp.AccessToken
}
return
}
// JoinRoom joins the client to a room ID or alias. See http://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-join-roomidoralias
//
// If serverName is specified, this will be added as a query param to instruct the homeserver to join via that server. If content is specified, it will

View File

@ -27,7 +27,7 @@ func ExampleClient_BuildBaseURL() {
// Output: https://matrix.org/_matrix/client/r0/directory/room/%23matrix:matrix.org?access_token=abcdef123456
}
// Compiled, not run.
// Retrieve the content of a m.room.name state event.
func ExampleClient_StateEvent() {
content := struct {
name string `json:"name"`
@ -37,3 +37,16 @@ func ExampleClient_StateEvent() {
panic(err)
}
}
// Login to a local homeserver. This will set Client.UserID and Client.AccessToken on success.
func ExampleClient_Login() {
cli, _ := NewClient("http://localhost:8008", "", "")
_, err := cli.Login(&ReqLogin{
Type: "m.login.password",
User: "alice",
Password: "wonderland",
}, true)
if err != nil {
panic(err)
}
}

View File

@ -9,3 +9,15 @@ type ReqRegister struct {
InitialDeviceDisplayName string `json:"initial_device_display_name"`
Auth interface{} `json:"auth,omitempty"`
}
// ReqLogin is the JSON request for http://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-login
type ReqLogin struct {
Type string `json:"type"`
Password string `json:"password,omitempty"`
Medium string `json:"medium,omitempty"`
User string `json:"user,omitempty"`
Address string `json:"address,omitempty"`
Token string `json:"token,omitempty"`
DeviceID string `json:"device_id,omitempty"`
InitialDeviceDisplayName string `json:"initial_device_display_name,omitempty"`
}

View File

@ -66,6 +66,14 @@ type RespRegister struct {
UserID string `json:"user_id"`
}
// RespLogin is the JSON response for http://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-login
type RespLogin struct {
AccessToken string `json:"access_token"`
DeviceID string `json:"device_id"`
HomeServer string `json:"home_server"`
UserID string `json:"user_id"`
}
// RespSync is the JSON response for http://matrix.org/docs/spec/client_server/r0.2.0.html#get-matrix-client-r0-sync
type RespSync struct {
NextBatch string `json:"next_batch"`