Module: FirebaseAdmin::Client::Accounts

Included in:
FirebaseAdmin::Client
Defined in:
lib/firebase-admin/client/accounts.rb

Overview

Defines methods related to accounts

Instance Method Summary collapse

Instance Method Details

#create_account(params) ⇒ Resource

Create a new account

)

Examples:

FirebaseAdmin.create_account(
  :email => "[email protected]",
  :password => "super-secret",
  :phoneNumber => "+5555555555",
  :displayName => "LeBron James",
  :photoUrl => "http://www.example.com/photo.jpg",
  :customAttributes => {
    :roles => ['admin']
  }

Parameters:

  • params (Hash)

    A customizable set of params

  • options (Hash)

    a customizable set of options

  • customAttributes (Hash)

    a customizable set of options

Returns:

  • (Resource)

See Also:



32
33
34
# File 'lib/firebase-admin/client/accounts.rb', line 32

def (params)
  post("v1/projects/#{project_id}/accounts", params)
end

#create_custom_token(uid) ⇒ String

Create a custom JWT token for a UID

Examples:

FirebaseAdmin.create_custom_token('...')

Parameters:

  • uid (String)

    The uid of a user

Returns:

  • (String)

See Also:



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/firebase-admin/client/accounts.rb', line 133

def create_custom_token(uid)
  if .nil? ||  == ''
    raise InvalidCredentials,
          "No client email provided via options, 'GOOGLE_APPLICATION_CREDENTIALS' or 'GOOGLE_CLIENT_EMAIL'"
  end

  if .nil? ||  == ''
    raise InvalidCredentials,
          "No private key provided via options, 'GOOGLE_APPLICATION_CREDENTIALS' or 'GOOGLE_PRIVATE_KEY'"
  end

  now_seconds = Time.now.to_i
  payload = {
    iss: ,
    sub: ,
    aud: 'https://identitytoolkit.googleapis.com/google.identity.identitytoolkit.v1.IdentityToolkit',
    iat: now_seconds,
    exp: now_seconds + (60 * 60), # Maximum expiration time is one hour
    uid: uid
  }
  JWT.encode(payload, OpenSSL::PKey::RSA.new(unescape()), 'RS256')
end

#delete_account(localId) ⇒ Object

DELETES the account completely from firebase. Once deleted we can retreive the account Need to be used cautiously

Parameters:

  • localId (String)

    profile ID

Returns:

  • 200 OK

See Also:



73
74
75
76
77
# File 'lib/firebase-admin/client/accounts.rb', line 73

def (localId)
  params = {'localId': localId}
  path = "v1/projects/#{project_id}/accounts:delete"
  post path, params
end

#get_user_by(key, value) ⇒ Resource

Get user by email/phone/uid

Examples:

FirebaseAdmin.get_user_by(:email, "[email protected]")

Parameters:

  • key (String)

    either :email or :phone

  • value (String)

    the value to search for

Returns:

  • (Resource)


165
166
167
168
169
170
# File 'lib/firebase-admin/client/accounts.rb', line 165

def get_user_by(key, value)
  params = {}
  params[key] = Array(value)
  response = post('v1/accounts:lookup', params)
  (response[:users] || []).first
end

#resetResource

Reset emulator



179
180
181
# File 'lib/firebase-admin/client/accounts.rb', line 179

def reset
  delete("emulator/v1/projects/#{project_id}/accounts")
end

#sign_in_for_uid(uid) ⇒ Resource

Sign in based on the UID of an account This generates a custom token for the UID and signs in using the custom token

Examples:

FirebaseAdmin.("...")

Parameters:

  • uid (String)

    The uid of a user

Returns:

  • (Resource)

    with idToken



119
120
121
122
# File 'lib/firebase-admin/client/accounts.rb', line 119

def (uid)
  custom_token = create_custom_token(uid)
  (custom_token)
end

#sign_in_with_custom_token(token) ⇒ Resource

Sign in with custom token

Examples:

FirebaseAdmin.("...")

Parameters:

  • token (String)

    A custom token

Returns:

  • (Resource)

    with idToken



106
107
108
# File 'lib/firebase-admin/client/accounts.rb', line 106

def (token)
  post('v1/accounts:signInWithCustomToken', { token: token, returnSecureToken: true })
end

#sign_in_with_password(params) ⇒ Resource

Sign in with a password

)

Examples:

FirebaseAdmin.sign_in_with_password(
  email: "[email protected]",
  password: "super-secret"

Parameters:

  • params (Hash)

    A customizable set of params

  • options (Hash)

    a customizable set of options

Returns:

  • (Resource)

See Also:



94
95
96
# File 'lib/firebase-admin/client/accounts.rb', line 94

def (params)
  post('v1/accounts:signInWithPassword', params)
end

#update_account(params) ⇒ Resource

Update an existing account

)

Examples:

FirebaseAdmin.update_account(
  email: "[email protected]",
  password: "super-secret",
  phoneNumber: "+5555555555",
  displayName: "LeBron James",
  photoUrl: "http://www.example.com/photo.jpg",
  localId: "1234",
  customAttributes: {
    :roles => ['admin']
  }

Parameters:

  • params (Hash)

    A customizable set of params

  • options (Hash)

    a customizable set of options

  • customAttributes (Hash)

    a customizable set of options

Returns:

  • (Resource)

See Also:



62
63
64
# File 'lib/firebase-admin/client/accounts.rb', line 62

def (params)
  post("v1/projects/#{project_id}/accounts:update", params)
end