Sha256: 22fa867a9ac838233362929303d41b64ff7ac13e074c6696b218f4c1da330e25

Contents?: true

Size: 1.95 KB

Versions: 3

Compression:

Stored size: 1.95 KB

Contents

require 'email_validator'

module Authenticate
  module Model

    # Use :email as the identifier for the user. Email must be unique.
    #
    # = Columns
    # * email - the email address of the user
    #
    # = Validations
    # * :email - require email is set, is a valid format, and is unique
    #
    # = Callbacks
    #
    # = Methods
    # * normalize_email - normalize the email, removing spaces etc, before saving
    #
    # = Class Methods
    # * credentials(params) - return the credentials required for authorization by email
    # * authenticate(credentials) - find user with given email, validate their password, return the user if authenticated
    # * normalize_email(email) - clean up the given email and return it.
    # * find_by_credentials(credentials) - find and return the user with the email address in the credentials
    #
    module Email
      extend ActiveSupport::Concern

      def self.required_fields(klass)
        [:email]
      end

      included do
        before_validation :normalize_email
        validates :email,
                  email: { strict_mode: true },
                  presence: true,
                  uniqueness: { allow_blank: true }
      end


      module ClassMethods

        def credentials(params)
          [params[:session][:email], params[:session][:password]]
        end

        def authenticate(credentials)
          user = find_by_credentials(credentials)
          user && user.password_match?(credentials[1]) ? user : nil
        end

        def find_by_credentials(credentials)
          email = credentials[0]
          find_by_email normalize_email(email)
        end

        def normalize_email(email)
          email.to_s.downcase.gsub(/\s+/, '')
        end

      end

      # Sets the email on this instance to the value returned by
      # {.normalize_email}
      #
      # @return [String]
      def normalize_email
        self.email = self.class.normalize_email(email)
      end
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
authenticate-0.2.3 lib/authenticate/model/email.rb
authenticate-0.2.2 lib/authenticate/model/email.rb
authenticate-0.2.1 lib/authenticate/model/email.rb