Class: Firebase::Auth::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key) ⇒ Client

Returns a new instance of Client.

Raises:

  • (ArgumentError)


11
12
13
14
15
# File 'lib/firebase/auth.rb', line 11

def initialize(api_key)
  raise ArgumentError.new('Missing api_key') if (api_key.nil? || api_key.empty?)

  @api_key = api_key
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



9
10
11
# File 'lib/firebase/auth.rb', line 9

def api_key
  @api_key
end

Instance Method Details

#change_email(token, email) ⇒ Object

Change a user’s email Params:

@token: user's token
@email: the user's new email.

Error

[EMAIL_EXISTS, INVALID_ID_TOKEN]


128
129
130
131
132
# File 'lib/firebase/auth.rb', line 128

def change_email(token, email)
  data = { idToken: token, email: email, returnSecureToken: true}

  process(:post, Config::SET_ACCOUNT_INFO, data)
end

#change_password(token, password) ⇒ Object

Change a user’s password Params:

@token: user's token
@password: User's new password.

Error

[INVALID_ID_TOKEN, WEAK_PASSWORD]


140
141
142
143
144
# File 'lib/firebase/auth.rb', line 140

def change_password(token, password)
  data = { idToken: token, password: password, returnSecureToken: true }

  process(:post, Config::SET_ACCOUNT_INFO, data)
end

#confirm_email(code) ⇒ Object

Verify email by code Params:

@code:

Error

[EXPIRED_OOB_CODE, INVALID_OOB_CODE,
  USER_DISABLED, EMAIL_NOT_FOUND]


238
239
240
241
242
# File 'lib/firebase/auth.rb', line 238

def confirm_email(code)
  data = { oobCode: code }

  process(:post, Config::SET_ACCOUNT_INFO, data)
end

#delete_account(token) ⇒ Object



244
245
246
247
248
# File 'lib/firebase/auth.rb', line 244

def (token)
  data = { idToken: token }

  process(:post, Config::DELETE_ACCOUNT, data)
end

#fetch_providers_for_email(email, continue_uri) ⇒ Object

Fetch providers for email Params:

@email: User's email address
@continue_uri: The URI to which the IDP redirects the user back

Error

[INVALID_EMAIL]


79
80
81
82
83
# File 'lib/firebase/auth.rb', line 79

def fetch_providers_for_email(email, continue_uri)
  data = { identifier: email, continueUri: continue_uri }

  process(:post, Config::GET_PROVIDERS_FOR_EMAIL, data)
end

#get_account_info(token) ⇒ Object

Get account info Params:

@token: The Firebase ID token of the account

Error

[INVALID_ID_TOKEN, USER_NOT_FOUND]


167
168
169
170
171
# File 'lib/firebase/auth.rb', line 167

def (token)
  data = { idToken: token }

  process(:post, Config::GET_ACCOUNT_INFO, data)
end

#get_certificateObject

Additional Get certificate sign token (JWT)



268
269
270
271
272
273
274
275
276
277
# File 'lib/firebase/auth.rb', line 268

def get_certificate
  begin
    res = RestClient::Request.execute(method: :get,
                              url: "#{Config::GET_CERTIFICATE}",
                              timeout: 10)
    certificates = JSON.parse(res.body)
  rescue RestClient::ExceptionWithResponse => e
    e.response
  end
end

Link new account (email/ password) with user exist Param:

@token: token of main user
@email: user's email want link
@password: user's password want link

Error

[CREDENTIAL_TOO_OLD_LOGIN_AGAIN, TOKEN_EXPIRED,
  INVALID_ID_TOKEN, WEAK_PASSWORD]


181
182
183
184
185
186
187
188
189
# File 'lib/firebase/auth.rb', line 181

def link_with_email(token, email, password)
  data = {
    idToken: token,
    email: email, password: password,
    returnSecureToken: true
  }

  process(:post, Config::SET_ACCOUNT_INFO, data)
end

Link new account (email/ password) with user exist Param:

@token: token of main user
@email: user's email want link
@password: user's password want link

Error

[CREDENTIAL_TOO_OLD_LOGIN_AGAIN, TOKEN_EXPIRED,
  INVALID_ID_TOKEN, WEAK_PASSWORD]


199
200
201
202
203
204
205
206
207
# File 'lib/firebase/auth.rb', line 199

def link_with_oauth(token, provider, access_token, redirect_uri)
  data = {
    idToken: token, requestUri: redirect_uri,
    postBody: "id_token=#{access_token}&providerId=#{provider}",
    returnSecureToken: true, returnIdpCredential: true
  }

  process(:post, Config::SET_ACCOUNT_INFO, data)
end

#refresh_token(refresh_token) ⇒ Object



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/firebase/auth.rb', line 250

def refresh_token(refresh_token)
  data = {
    grant_type: Param::REFRESH_TOKEN,
    refresh_token: refresh_token
  }

  begin
    RestClient::Request.execute(method: :post,
                              url: "#{Config::EXCHANGE_REFRESH_TOKEN}?key=#{api_key}",
                              headers: { 'Content-Type': 'application/json' },
                              payload: data.to_json,
                              timeout: 10)
  rescue RestClient::ExceptionWithResponse => e
    e.response
  end
end

#reset_password(code, password) ⇒ Object

Reset password Params:

@code: code received though email
@password: new password want change

Error

[OPERATION_NOT_ALLOWED, EXPIRED_OOB_CODE, INVALID_OOB_CODE]


116
117
118
119
120
# File 'lib/firebase/auth.rb', line 116

def reset_password(code, password)
  data = { oobCode: code, newPassword: password }

  process(:post, Config::RESET_PASSWORD, data)
end

#send_confirm_code(email) ⇒ Object

Send code confirmation to reset password via email Params:

@email: User's email address want reset password

Error

[EMAIL_NOT_FOUND]


90
91
92
93
94
95
96
97
# File 'lib/firebase/auth.rb', line 90

def send_confirm_code(email)
  data = {
    email: email,
    requestType: Param::PASSWORD_RESET
  }

  process(:post, Config::SEND_CODE_CONFIRM, data)
end

#send_email_verify(token) ⇒ Object

Send email verification Params:

 @token: user's token
Error
 [INVALID_ID_TOKEN, USER_NOT_FOUND]


226
227
228
229
230
# File 'lib/firebase/auth.rb', line 226

def send_email_verify(token)
  data = { idToken: token, requestType: Param::VERIFY_EMAIL }

  process(:post, Config::SEND_CODE_CONFIRM, data)
end

#sign_in_anonymouslyObject

Sign in a user anonymously



50
51
52
53
# File 'lib/firebase/auth.rb', line 50

def 
  data = { returnSecureToken: true }
  process(:post, Config::SIGN_UP_EMAIL, data)
end

#sign_in_email(email, password) ⇒ Object

Sign in a user with an email and password # Params:

@email:
@password:

Error:

[EMAIL_NOT_FOUND, INVALID_PASSWORD, USER_DISABLED]

Referrences:

https://firebase.google.com/docs/reference/rest/auth/#section-create-email-password


42
43
44
45
46
47
# File 'lib/firebase/auth.rb', line 42

def (email, password)
  data = {
    email: email, password: password, returnSecureToken: true
  }
  process(:post, Config::SIGN_IN_EMAIL, data)
end

#sign_in_oauth(provider, access_token, request_uri) ⇒ Object

Sign in with OAuth credential Params:

@provider: [facebook.com, google.com, github.com, twitter.com]
@access_token: OAuth credential
@request_uri: The URI to which the IDP redirects the user back.

Error

[OPERATION_NOT_ALLOWED, INVALID_IDP_RESPONSE]


62
63
64
65
66
67
68
69
70
71
# File 'lib/firebase/auth.rb', line 62

def (provider, access_token, request_uri)
  data = {
    requestUri: request_uri,
    postBody: "access_token=#{access_token}&providerId=#{provider}",
    returnSecureToken: true,
    returnIdpCredential: true
  }

  process(:post, Config::SIGN_IN_OAUTH, data)
end

#sign_up_email(email, password, options = {}) ⇒ Object

Create a new email and password user Params:

@email: The email for the user to create.
@password: The password for the user to create.

Error:

[EMAIL_EXISTS, OPERATION_NOT_ALLOWED, TOO_MANY_ATTEMPTS_TRY_LATER]

Referrences:

https://firebase.google.com/docs/reference/rest/auth/#section-create-email-password


25
26
27
28
29
30
31
32
# File 'lib/firebase/auth.rb', line 25

def (email, password, options={})
  data = {
    email: email, password: password,
    displayName: options[:display_name],
    returnSecureToken: true
  }
  process(:post, Config::SIGN_UP_EMAIL, data)
end

Unlink between account Params:

@token: user's token want unlink
@providers: list of provider want unlink

Error

[INVALID_ID_TOKEN]


215
216
217
218
219
# File 'lib/firebase/auth.rb', line 215

def unlink_provider(token, providers=[])
  data = { idToken: token, deleteProvider: providers }

  process(:post, Config::SET_ACCOUNT_INFO, data)
end

#update_profile(token, user_name, photo_url) ⇒ Object

Update user’s profile Params:

@token: user's token
@user_name: User's new display name
@photo_url: User's new photo url

Error

[INVALID_ID_TOKEN]


153
154
155
156
157
158
159
160
# File 'lib/firebase/auth.rb', line 153

def update_profile(token, user_name, photo_url)
  data = {
    idToken: token, displayName: user_name,
    photoUrl: photo_url, returnSecureToken: true
  }

  process(:post, Config::SET_ACCOUNT_INFO, data)
end

#verfify_password_code(code) ⇒ Object

Verify password reset code Params:

@code: code received though email

Error

[OPERATION_NOT_ALLOWED, EXPIRED_OOB_CODE, INVALID_OOB_CODE]


104
105
106
107
108
# File 'lib/firebase/auth.rb', line 104

def verfify_password_code(code)
  data = { oobCode: code }

  process(:post, Config::VERIFY_CODE_RESET_PWD, data)
end