Sha256: 4f992d9059b19b5ad213784bcc935b95af1d534aafdae9d8da4ae5075ce0299c

Contents?: true

Size: 881 Bytes

Versions: 1

Compression:

Stored size: 881 Bytes

Contents

module JT::User::Authentication
	extend ActiveSupport::Concern

	included do

		has_secure_password

		tokenize :password_token

		validates :email, presence: true, email_format: true, uniqueness: { case_sensitive: false }
		
		before_save :downcase_email

		scope :search_by_email, ->(email) { where(email: email.to_s.downcase) }
		scope :search_by_email_for_authentication, ->(email) { search_by_email(email).where.not(password_digest: nil) }

	end

	class_methods do

		def authenticate(email, password)
			self.search_by_email_for_authentication(email).first.try(:authenticate, password)
		end

	end

	def downcase_email
		self.email.downcase! if self.email
	end

	def increment_login_stats!(remote_ip)
		attributes = {
			last_login_at: Time.now,
			last_login_remote_ip: remote_ip,
			login_count: (self.login_count || 0) + 1
		}

		self.update_columns(attributes)
	end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
jt-rails-generator-user-1.1.2 lib/generators/jt/user/templates/models/concerns/jt/user/authentication.rb