Class: Firebase::Admin::Auth::UserManager

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/firebase/admin/auth/user_manager.rb

Overview

Provides methods for interacting with the Google Identity Toolkit

Constant Summary

Constants included from Utils

Firebase::Admin::Auth::Utils::AUTH_EMULATOR_HOST_VAR, Firebase::Admin::Auth::Utils::HOSTNAME_PATTERN, Firebase::Admin::Auth::Utils::INVALID_CHARS_PATTERN, Firebase::Admin::Auth::Utils::PATHNAME_PATTERN

Instance Method Summary collapse

Methods included from Utils

get_emulator_host, get_emulator_v1_url, is_emulated?, #to_boolean, #validate_display_name, #validate_email, #validate_password, #validate_phone_number, #validate_photo_url, #validate_uid, validate_url

Constructor Details

#initialize(project_id, credentials, url_override = nil) ⇒ UserManager

Initializes a UserManager.

Parameters:

  • project_id (String)

    The Firebase project id.

  • credentials (Credentials)

    The credentials to authenticate with.

  • url_override (String, nil) (defaults to: nil)

    The base url to override with.



14
15
16
17
18
# File 'lib/firebase/admin/auth/user_manager.rb', line 14

def initialize(project_id, credentials, url_override = nil)
  uri = "#{url_override || ID_TOOLKIT_URL}/"
  @project_id = project_id
  @client = Firebase::Admin::Internal::HTTPClient.new(uri: uri, credentials: credentials)
end

Instance Method Details

#create_user(uid: nil, display_name: nil, email: nil, email_verified: nil, phone_number: nil, photo_url: nil, password: nil, disabled: nil) ⇒ UserRecord

Creates a new user account with the specified properties.

Parameters:

  • uid (String, nil) (defaults to: nil)

    The id to assign to the newly created user.

  • display_name (String, nil) (defaults to: nil)

    The user’s display name.

  • email (String, nil) (defaults to: nil)

    The user’s primary email.

  • email_verified (Boolean, nil) (defaults to: nil)

    A boolean indicating whether or not the user’s primary email is verified.

  • phone_number (String, nil) (defaults to: nil)

    The user’s primary phone number.

  • photo_url (String, nil) (defaults to: nil)

    The user’s photo URL.

  • password (String, nil) (defaults to: nil)

    The user’s raw, unhashed password.

  • disabled (Boolean, nil) (defaults to: nil)

    A boolean indicating whether or not the user account is disabled.

Returns:

Raises:



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/firebase/admin/auth/user_manager.rb', line 34

def create_user(uid: nil, display_name: nil, email: nil, email_verified: nil, phone_number: nil, photo_url: nil, password: nil, disabled: nil)
  payload = {
    localId: validate_uid(uid),
    displayName: validate_display_name(display_name),
    email: validate_email(email),
    phoneNumber: validate_phone_number(phone_number),
    photoUrl: validate_photo_url(photo_url),
    password: validate_password(password),
    emailVerified: to_boolean(email_verified),
    disabled: to_boolean(disabled)
  }.compact
  res = @client.post(with_path("accounts"), payload).body
  uid = res&.fetch("localId")
  raise CreateUserError, "failed to create user #{res}" if uid.nil?
  get_user_by(uid: uid)
end

#delete_user(uid) ⇒ Object

Deletes the user corresponding to the specified user id.

Parameters:

  • uid (String)

    The id of the user.



78
79
80
# File 'lib/firebase/admin/auth/user_manager.rb', line 78

def delete_user(uid)
  @client.post(with_path("accounts:delete"), {localId: validate_uid(uid, required: true)})
end

#get_user_by(query) ⇒ UserRecord

Gets the user corresponding to the provided key

Parameters:

  • query (Hash)

    Query parameters to search for a user by.

Options Hash (query):

  • :uid (String)

    A user id.

  • :email (String)

    An email address.

  • :phone_number (String)

    A phone number.

Returns:



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/firebase/admin/auth/user_manager.rb', line 59

def get_user_by(query)
  if (uid = query[:uid])
    payload = {localId: Array(validate_uid(uid, required: true))}
  elsif (email = query[:email])
    payload = {email: Array(validate_email(email, required: true))}
  elsif (phone_number = query[:phone_number])
    payload = {phoneNumber: Array(validate_phone_number(phone_number, required: true))}
  else
    raise ArgumentError, "Unsupported query: #{query}"
  end
  res = @client.post(with_path("accounts:lookup"), payload).body
  users = res["users"] if res
  UserRecord.new(users[0]) if users.is_a?(Array) && users.length > 0
end