Method: MatrixSdk::Protocols::CS#login

Defined in:
lib/matrix_sdk/protocols/cs.rb

#login(login_type: 'm.login.password', **params) ⇒ Response

Logs in using the client API /login endpoint, and optionally stores the resulting access for API usage

Examples:

Logging in with username and password

api.(user: 'example', password: 'NotARealPass')
# => { user_id: '@example:matrix.org', access_token: '...', home_server: 'matrix.org', device_id: 'ABCD123' }
api.whoami?
# => { user_id: '@example:matrix.org' }

Advanced login, without storing details

api.whoami?
# => { user_id: '@example:matrix.org' }
api.(medium: 'email', address: '[email protected]', password: '...', store_token: false)
# => { user_id: '@someone:matrix.org', access_token: ...
api.whoami?.user_id
# => '@example:matrix.org'

Parameters:

  • login_type (String) (defaults to: 'm.login.password')

    (‘m.login.password’) The type of login to attempt

  • params (Hash)

    The login information to use, along with options for said log in

Options Hash (**params):

  • :store_token (Boolean) — default: true

    Should the resulting access token be stored for the API

  • :store_device_id (Boolean) — default: store_token value

    Should the resulting device ID be stored for the API

  • :initial_device_display_name (String) — default: USER_AGENT

    The device display name to specify for this login attempt

  • :device_id (String)

    The device ID to set on the login

Returns:

  • (Response)

    A response hash with the parameters :user_id, :access_token, :home_server, and :device_id.

See Also:



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/matrix_sdk/protocols/cs.rb', line 104

def (login_type: 'm.login.password', **params)
  query = {}
  query[:user_id] = params.delete(:user_id) if protocol?(:AS) && params.key?(:user_id)

  options = {}
  options[:store_token] = params.delete(:store_token) { true }
  options[:store_device_id] = params.delete(:store_device_id) { options[:store_token] }

  data = {
    type: ,
    initial_device_display_name: params.delete(:initial_device_display_name) { MatrixSdk::Api::USER_AGENT }
  }.merge params
  data[:device_id] = device_id if device_id

  request(:post, :client_r0, '/login', body: data, query: query).tap do |resp|
    @access_token = resp.token if resp.key?(:token) && options[:store_token]
    @device_id = resp.device_id if resp.key?(:device_id) && options[:store_device_id]
  end
end