mirror of
				https://github.com/matrix-org/gomatrix
				synced 2025-11-03 22:08:04 +00:00 
			
		
		
		
	
						commit
						16d753287d
					
				
					 4 changed files with 46 additions and 1 deletions
				
			
		
							
								
								
									
										12
									
								
								client.go
									
										
									
									
									
								
							
							
						
						
									
										12
									
								
								client.go
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -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
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										12
									
								
								requests.go
									
										
									
									
									
								
							
							
						
						
									
										12
									
								
								requests.go
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -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"`
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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"`
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue