Module: Authpwn::UserModel::ClassMethods

Defined in:
lib/authpwn_rails/user_model.rb

Overview

Class methods on models that include Authpwn::UserModel.

Instance Method Summary collapse

Instance Method Details

#authenticate_signin(signin) ⇒ User, Symbol

Authenticates a user given the information on a signup form.

The easiest method of accepting other login information is to override this method, locate the user’s email, and supply it in a call to super.

Parameters:

  • signin (Session)

    the information entered in the sign-in form

Returns:

  • (User, Symbol)

    the authenticated user, or a symbol indicating the reason why the authentication failed



51
52
53
# File 'lib/authpwn_rails/user_model.rb', line 51

def ()
  Credentials::Password.authenticate_email .email, .password
end

#create_from_omniauth(omniauth_hash) ⇒ User

Change this to customize on-demand user creation on OmniAuth signup.

This method is called when there is no existing user matching the OmniAuth information, and is responsible for creating a user. It is an opportunity to collect the OmniAuth information to populate the user’s account.

The default implementation creates a user with the e-mail matching the ‘email’ key in the OmniAuth hash. If no e-mail key is present, no User is created.

Parameters:

  • omniauth_hash (Hash)

    the hash provided by OmniAuth

Returns:

  • (User)

    a saved User, or nil if the OmniAuth sign-in information should not be used to create a user



88
89
90
91
92
93
94
95
# File 'lib/authpwn_rails/user_model.rb', line 88

def create_from_omniauth(omniauth_hash)
  info_hash = omniauth_hash['info']
  return nil unless email = info_hash && info_hash['email']
  user = User.new
  user.credentials << Credentials::Email.new(email: email, verified: true)
  user.save!
  user
end

Looks up the User tat may be related to an OmniAuth sign-in.

This method is called when there is no Credential matching the OmniAuth information, but before User#create_from_omniauth. It is an opportunity to identify an existing user who uses a new sign-in method.

The default implementation finds an user whose e-mail matches the ‘email’ value in the OmniAuth hash.

Parameters:

  • omniauth_hash (Hash)

    the hash provided by OmniAuth

Returns:

  • (User)

    the user who should be signed in, or nil if no such user exists



67
68
69
70
71
72
# File 'lib/authpwn_rails/user_model.rb', line 67

def related_to_omniauth(omniauth_hash)
  info_hash = omniauth_hash['info']
  return nil unless email = info_hash && info_hash['email']
  credential = Credentials::Email.with email
  credential and credential.user
end

#with_param(param) ⇒ ActiveRecord::Relation

Scope using the value returned by User#to_param.

Parameters:

  • param (String)

    value returned by User#to_param

Returns:

  • (ActiveRecord::Relation)


39
40
41
# File 'lib/authpwn_rails/user_model.rb', line 39

def with_param(param)
  where(exuid: param)
end