Module: Sinatra::Security::Identification

Defined in:
lib/sinatra/security/identification.rb

Overview

The identification module is mixed into Sinatra::Security::User. The API loosely applies to Ohm, although it will work with ActiveRecord.

If you wish to override any of these, it’s as simple as defining your own method in your User class.

Examples:


class User < Ohm::Model
  include Sinatra::Security::User
end

User.respond_to?(:find_by_login)
# => true

User.respond_to?(:authenticate)
# => true

Instance Method Summary collapse

Instance Method Details

#authenticate(login, password) ⇒ User?

Finds the User matching the given login / password combination.

Parameters:

  • login (#to_s)

    the value of ‘:email` in your datastore.

  • password (String)

    the raw password for the user in ‘:email`.

Returns:

  • (User)

    the user matching the credentials if found.

  • (nil)

    if no matching user found.

See Also:



28
29
30
31
32
33
34
# File 'lib/sinatra/security/identification.rb', line 28

def authenticate(, password)
  if user = ()
    if Sinatra::Security::Password::Hashing.check(password, user.crypted_password)
      user
    end
  end
end

#find_by_login(login) ⇒ User?

Used internally by User::authenticate to find the user given the ‘:email` value.

Parameters:

  • login (#to_s)

    the value of ‘:email` in your datastore. You may also override the key used.

Returns:

  • (User)

    an instance of User if found.

  • (nil)

    if no user found with the given login value.

See Also:

  • LoginField::attr_name


45
46
47
# File 'lib/sinatra/security/identification.rb', line 45

def ()
  find( => ).first
end