Class: Firebase::Authentication::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/firebase/authentication/service.rb

Overview

A Ruby wrapper for Firebase REST API

Query the Firebase Auth backend through a REST API

Examples:

require "firebase/authentication"

service = Firebase::Authentication::Service.new(ENV['API_KEY'])
service.(email, password)

See Also:

Instance Method Summary collapse

Constructor Details

#initialize(api_key, logger = Rails.logger) ⇒ Service

Returns a new instance of Service.



21
22
23
24
# File 'lib/firebase/authentication/service.rb', line 21

def initialize(api_key, logger = Rails.logger)
  @logger = logger
  @api_key = api_key
end

Instance Method Details

#change_email(token, email) ⇒ Net::HTTPOK

Change a user’s email.

Parameters:

  • token (String)

    A Firebase Auth ID token for the user.

  • email (String)

    The user’s new email.

Returns:

  • (Net::HTTPOK)

Raises:

  • (Net::HTTPRetriableError)

    An error occurred on the server and the request can be retried

  • (Net::HTTPServerException)

    The request is invalid and should not be retried without modification

  • (Net::HTTPFatalError)

    An internal server error occurred

See Also:



39
40
41
42
43
# File 'lib/firebase/authentication/service.rb', line 39

def change_email(token, email)
  res = fetch(:post, Config::UPDATE_ACCOUNT_INFO, { idToken: token, email: email, returnSecureToken: true })
  res.value
  res
end

#change_password(token, password) ⇒ Net::HTTPOK

Change a user’s password.

Parameters:

  • token (String)

    A Firebase Auth ID token for the user.

  • password (String)

    The user’s new password.

Returns:

  • (Net::HTTPOK)

Raises:

  • (Net::HTTPRetriableError)

    An error occurred on the server and the request can be retried

  • (Net::HTTPServerException)

    The request is invalid and should not be retried without modification

  • (Net::HTTPFatalError)

    An internal server error occurred

See Also:



58
59
60
61
62
# File 'lib/firebase/authentication/service.rb', line 58

def change_password(token, password)
  res = fetch(:post, Config::UPDATE_ACCOUNT_INFO, { idToken: token, passsord: password, returnSecureToken: true })
  res.value
  res
end

#delete_account(token) ⇒ Net::HTTPOK

Delete a user.

Parameters:

  • token (String)

    The Firebase ID token of the user to delete.

Returns:

  • (Net::HTTPOK)

Raises:

  • (Net::HTTPRetriableError)

    An error occurred on the server and the request can be retried

  • (Net::HTTPServerException)

    The request is invalid and should not be retried without modification

  • (Net::HTTPFatalError)

    An internal server error occurred

See Also:



75
76
77
78
79
# File 'lib/firebase/authentication/service.rb', line 75

def (token)
  res = fetch(:post, Config::DELETE_ACCOUNT, { idToken: token })
  res.value
  res
end

#exchange_custom_token(token) ⇒ Net::HTTPOK

Exchange a custom Auth token for an ID and refresh token

Parameters:

  • token (String)

    A Firebase Auth custom token from which to create an ID and refresh token pair.

Returns:

  • (Net::HTTPOK)

Raises:

  • (Net::HTTPRetriableError)

    An error occurred on the server and the request can be retried

  • (Net::HTTPServerException)

    The request is invalid and should not be retried without modification

  • (Net::HTTPFatalError)

    An internal server error occurred

See Also:



92
93
94
95
96
# File 'lib/firebase/authentication/service.rb', line 92

def exchange_custom_token(token)
  res = fetch(:post, Config::VERIFY_CUSTOM_TOKEN, { idToken: token, returnSecureToken: true })
  res.value
  res
end

#get_account_info(token) ⇒ Net::HTTPOK

Get a user’s data

Parameters:

  • token (String)

    The Firebase ID token of the account.

Returns:

  • (Net::HTTPOK)

Raises:

  • (Net::HTTPRetriableError)

    An error occurred on the server and the request can be retried

  • (Net::HTTPServerException)

    The request is invalid and should not be retried without modification

  • (Net::HTTPFatalError)

    An internal server error occurred

See Also:



109
110
111
112
113
# File 'lib/firebase/authentication/service.rb', line 109

def (token)
  res = fetch(:post, Config::GET_ACCOUNT_INFO, { idToken: token })
  res.value
  res
end

#send_password_reset_email(email) ⇒ Net::HTTPOK

Send a password reset email.

Parameters:

  • email (String)

    User’s email address.

Returns:

  • (Net::HTTPOK)

Raises:

  • (Net::HTTPRetriableError)

    An error occurred on the server and the request can be retried

  • (Net::HTTPServerException)

    The request is invalid and should not be retried without modification

  • (Net::HTTPFatalError)

    An internal server error occurred

See Also:



126
127
128
129
130
# File 'lib/firebase/authentication/service.rb', line 126

def send_password_reset_email(email)
  res = fetch(:post, Config::RESET_PASSWORD, { requestType: "PASSWORD_RESET", email: email })
  res.value
  res
end

#sign_in_email(email, password) ⇒ Net::HTTPOK

Signin a user.

Parameters:

  • email (String)

    The email the user is signing in with.

  • password (String)

    The password for the account.

Returns:

  • (Net::HTTPOK)

Raises:

  • (Net::HTTPRetriableError)

    An error occurred on the server and the request can be retried

  • (Net::HTTPServerException)

    The request is invalid and should not be retried without modification

  • (Net::HTTPFatalError)

    An internal server error occurred

See Also:



145
146
147
148
149
# File 'lib/firebase/authentication/service.rb', line 145

def (email, password)
  res = fetch(:post, Config::SIGN_IN_EMAIL, { email: email, passsord: password, returnSecureToken: true })
  res.value
  res
end

#sign_up(email, password) ⇒ Net::HTTPOK

Signup new user.

Parameters:

  • email (String)

    The email for the user to create.

  • password (String)

    The password for the user to create.

Returns:

  • (Net::HTTPOK)

Raises:

  • (Net::HTTPRetriableError)

    An error occurred on the server and the request can be retried

  • (Net::HTTPServerException)

    The request is invalid and should not be retried without modification

  • (Net::HTTPFatalError)

    An internal server error occurred

See Also:



164
165
166
167
168
# File 'lib/firebase/authentication/service.rb', line 164

def (email, password)
  res = fetch(:post, Config::SIGN_UP_EMAIL, { email: email, password: password, returnSecureToken: true })
  res.value
  res
end