Module: Parse::MFA::UserExtension::ClassMethods

Defined in:
lib/parse/two_factor_auth/user_extension.rb

Overview

Class methods added to Parse::User

Instance Method Summary collapse

Instance Method Details

#login_with_mfa(username, password, mfa_token) ⇒ User?

Login a user with username, password, and MFA token.

This method handles the Parse Server MFA “additional” policy, which requires both standard credentials AND an MFA token.

Examples:

user = Parse::User.("john", "password123", "123456")

Parameters:

  • username (String)

    The username

  • password (String)

    The password

  • mfa_token (String)

    The TOTP code from authenticator app or recovery code

Returns:

  • (User, nil)

    The logged in user or nil if failed

Raises:



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/parse/two_factor_auth/user_extension.rb', line 50

def (username, password, mfa_token)
  raise MFA::RequiredError, "MFA token is required" if mfa_token.blank?

  response = client.(username, password, mfa_token)
  return nil unless response.success?

  Parse::User.build(response.result)
rescue Parse::Client::ResponseError => e
  if e.message.include?("Invalid MFA token") || e.message.include?("Missing additional authData")
    raise MFA::VerificationError, e.message
  end
  raise
end

#mfa_required?(username) ⇒ Boolean

Check if a user requires MFA for login.

This queries the user’s authData.mfa status using the afterFind hook which returns { status: “enabled” } or { status: “disabled” }.

Examples:

if Parse::User.mfa_required?("john")
  # Show MFA input field
end

Parameters:

  • username (String)

    The username to check

Returns:

  • (Boolean)

    True if MFA is required



76
77
78
79
80
81
# File 'lib/parse/two_factor_auth/user_extension.rb', line 76

def mfa_required?(username)
  user = where(username: username).first
  return false unless user

  user.mfa_enabled?
end