Module: Auth0::Api::V2::Users

Included in:
Auth0::Api::V2
Defined in:
lib/auth0/api/v2/users.rb

Overview

Methods to use the users endpoints

Instance Method Summary collapse

Instance Method Details

#create_user(name, options = {}) ⇒ json

Creates a new user according to optional parameters received. The attribute connection is always mandatory but depending on the type of connection you are using there could be others too. For instance, Auth0 DB Connections require email and password.

Parameters:

  • name (string)

    The user name.

  • options (hash) (defaults to: {})
    • :connection [string] The connection the user belongs to.

Returns:

  • (json)

    Returns the created user.

See Also:


48
49
50
51
52
# File 'lib/auth0/api/v2/users.rb', line 48

def create_user(name, options = {})
  request_params = Hash[options.map { |(k, v)| [k.to_sym, v] }]
  request_params[:name] = name
  post(users_path, request_params)
end

#delete_user(user_id) ⇒ Object

Deletes a single user given its id

Parameters:

  • user_id (string)

    The user_id of the user to delete.

Raises:

See Also:


80
81
82
83
84
# File 'lib/auth0/api/v2/users.rb', line 80

def delete_user(user_id)
  raise Auth0::MissingUserId, 'Must supply a valid user_id' if user_id.to_s.empty?
  path = "#{users_path}/#{user_id}"
  delete(path)
end

#delete_user_provider(user_id, provider_name) ⇒ Object

Delete a user's multifactor provider

Parameters:

  • user_id (string)

    The user_id of the user to delete the multifactor provider from.

  • provider_name (string)

    The multifactor provider. Supported values 'duo' or 'google-authenticator'.

Raises:

See Also:


114
115
116
117
118
119
# File 'lib/auth0/api/v2/users.rb', line 114

def delete_user_provider(user_id, provider_name)
  raise Auth0::MissingUserId, 'Must supply a valid user_id' if user_id.to_s.empty?
  raise Auth0::InvalidParameter, 'Must supply a valid provider name' if provider_name.to_s.empty?
  path = "#{users_path}/#{user_id}/multifactor/#{provider_name}"
  delete(path)
end

#delete_usersObject

Delete all users - USE WITH CAUTION


56
57
58
# File 'lib/auth0/api/v2/users.rb', line 56

def delete_users
  delete(users_path)
end

Links the account specified in the body (secondary account) to the account specified by the id param of the URL (primary account).

  1. With the authenticated primary account's JWT in the Authorization header, which has the

update:current_user_identities scope. In this case only the link_with param is required in the body, containing the JWT obtained upon the secondary account's authentication.

  1. With an API V2 generated token with update:users scope. In this case you need to send provider and user_id

in the body. Optionally you can also send the connection_id param which is suitable for identifying a particular database connection for the 'auth0' provider.

Parameters:

  • user_id (string)

    The user_id of the primary identity where you are linking the secondary account to.

  • body (string)

    the options to link the account to.

Returns:

  • (json)

    Returns the new array of the primary account identities.

Raises:

See Also:


134
135
136
137
138
139
# File 'lib/auth0/api/v2/users.rb', line 134

def (user_id, body)
  raise Auth0::MissingUserId, 'Must supply a valid user_id' if user_id.to_s.empty?
  raise Auth0::InvalidParameter, 'Must supply a valid body' if body.to_s.empty?
  path = "#{users_path}/#{user_id}/identities"
  post(path, body)
end

#patch_user(user_id, body) ⇒ json Also known as: update_user

Updates a user with the object's properties received in the optional parameters. These are the attributes that can be updated at the root level: blocked, email_verified, email, verify_email, password, phone_number, phone_verified, verify_password, user_metadata, app_metadata, username Some considerations: The properties of the new object will replace the old ones. The metadata fields are an exception to this rule (user_metadata and app_metadata). These properties are merged instead of being replaced but be careful, the merge only occurs on the first level. If you are updating email_verified, phone_verified, username or password you need to specify the connection property too. If your are updating email or phone_number you need to specify the connection and the client_id properties.

Parameters:

  • user_id (string)

    The user_id of the user to update.

  • body (hash)

    The optional parametes to update.

Returns:

  • (json)

    Returns the updated user.

Raises:

See Also:


102
103
104
105
106
107
# File 'lib/auth0/api/v2/users.rb', line 102

def patch_user(user_id, body)
  raise Auth0::MissingUserId, 'Must supply a valid user_id' if user_id.to_s.empty?
  raise Auth0::InvalidParameter, 'Must supply a valid body' if body.to_s.empty? || body.empty?
  path = "#{users_path}/#{user_id}"
  patch(path, body)
end

Unlink a user account

Parameters:

  • user_id (string)

    The user_id of the user identity.

  • provider (string)

    The type of identity provider.

  • secondary_user_id (string)

    The unique identifier for the user for the identity.

Returns:

  • (json)

    Returns the array of the unlinked account identities.

Raises:

See Also:


148
149
150
151
152
153
154
# File 'lib/auth0/api/v2/users.rb', line 148

def (user_id, provider, secondary_user_id)
  raise Auth0::MissingUserId, 'Must supply a valid user_id' if user_id.to_s.empty?
  raise Auth0::MissingUserId, 'Must supply a valid secondary user_id' if secondary_user_id.to_s.empty?
  raise Auth0::InvalidParameter, 'Must supply a valid provider' if provider.to_s.empty?
  path = "#{users_path}/#{user_id}/identities/#{provider}/#{secondary_user_id}"
  delete(path)
end

#user(user_id, fields: nil, include_fields: true) ⇒ json

Retrieves a user given a user_id

Parameters:

  • user_id (string)

    The user_id of the user to retrieve.

  • fields (string)

    A comma separated list of fields to include or exclude from the result.

  • include_fields (boolean)

    True if the fields specified are to be included in the result, false otherwise.

Returns:

  • (json)

    Returns the user with the given user_id if it exists.

Raises:

See Also:


67
68
69
70
71
72
73
74
75
# File 'lib/auth0/api/v2/users.rb', line 67

def user(user_id, fields: nil, include_fields: true)
  raise Auth0::MissingUserId, 'Must supply a valid user_id' if user_id.to_s.empty?
  path = "#{users_path}/#{user_id}"
  request_params = {
    fields:         fields,
    include_fields: include_fields
  }
  get(path, request_params)
end

#user_logs(user_id, options = {}) ⇒ json Also known as: get_user_log_events

Retrieve every log event for a specific user id rubocop:disable Metrics/MethodLength, Metrics/AbcSize

Parameters:

  • user_id (string)

    The user_id of the logs to retrieve.

  • options (hash) (defaults to: {})
    • :per_page [integer] The amount of entries per page. Default: 50. Max value: 100.

    • :page [integer] The page number. Zero based.

    • :include_totals [boolean] True if a query summary must be included in the result.

    • :sort [string] The field to use for sorting. 1 == ascending and -1 == descending.

Returns:

  • (json)

    Returns the list of existing log entries for the given user_id.

Raises:

See Also:


167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/auth0/api/v2/users.rb', line 167

def user_logs(user_id, options = {})
  raise Auth0::MissingUserId, 'Must supply a valid user_id' if user_id.to_s.empty?
  path = "#{users_path}/#{user_id}/logs"
  request_params = {
    user_id:        user_id,
    per_page:       options.fetch(:per_page, nil),
    page:           options.fetch(:page, nil),
    include_totals: options.fetch(:include_totals, nil),
    sort:           options.fetch(:sort, nil)
  }
  if request_params[:per_page].to_i > 100
    raise Auth0::InvalidParameter, 'The total amount of entries per page should be less than 100'
  end
  sort_pattern = /^(([a-zA-Z0-9_\.]+))\:(1|-1)$/
  if !request_params[:sort].nil? && !sort_pattern.match(request_params[:sort])
    raise Auth0::InvalidParameter, 'Sort does not match pattern ^(([a-zA-Z0-9_\\.]+))\\:(1|-1)$'
  end
  get(path, request_params)
end

#users(options = {}) ⇒ json Also known as: get_users

Retrieves a list of Auth0 users.

Parameters:

  • options (hash) (defaults to: {})

    The Hash options used to refine the User results.

    • :per_page [integer] The amount of entries per page. Default: 50. Max value: 100.

    • :page [integer] The page number. Zero based.

    • :include_totals [boolean] True if a query summary must be included in the result.

    • :sort [string] The field to use for sorting. 1 == ascending and -1 == descending.

    • :connection [string] Connection to filter results by.

    • :fields [string] A comma separated list of result fields.

    • :include_fields [boolean] True to include :fields, false to exclude.

    • :q [string] Query in Lucene query string syntax.

    • :search_engine [string] User search engine version.

Returns:

  • (json)

    Returns the list of existing users.

See Also:


24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/auth0/api/v2/users.rb', line 24

def users(options = {})
  request_params = {
    per_page:       options.fetch(:per_page, nil),
    page:           options.fetch(:page, nil),
    include_totals: options.fetch(:include_totals, nil),
    sort:           options.fetch(:sort, nil),
    connection:     options.fetch(:connection, nil),
    fields:         options.fetch(:fields, nil),
    include_fields: options.fetch(:include_fields, nil),
    q:              options.fetch(:q, nil),
    search_engine:  options.fetch(:search_engine, nil)
  }
  get(users_path, request_params)
end