module ActsAsTokenable::Tokenable

Public Class Methods

find_by_token(token_id, token_hash) click to toggle source
# File lib/acts_as_tokenable/tokenable.rb, line 14
def find_by_token(token_id, token_hash)
  token_record = ActsAsTokenable::Token.active.find_by(:token_id => token_id)

  return nil unless token_record

  if BCrypt::Password.new(token_hash) == token_record.token
    token_record.try(:tokenable)
  else
    nil
  end
end

Public Instance Methods

acts_as_tokenable() click to toggle source
# File lib/acts_as_tokenable/tokenable.rb, line 3
def acts_as_tokenable
  class_eval do
    has_many :tokens, as: :tokenable, dependent: :destroy, class_name: '::ActsAsTokenable::Token', :foreign_key => :tokenable_id

    def add_token(options = {})
      options = options.reverse_merge(default_options)

      self.tokens.create(options)
    end

    class << self
      def find_by_token(token_id, token_hash)
        token_record = ActsAsTokenable::Token.active.find_by(:token_id => token_id)

        return nil unless token_record

        if BCrypt::Password.new(token_hash) == token_record.token
          token_record.try(:tokenable)
        else
          nil
        end
      end
    end

    private
      def default_options
        {
          :expires_at => 30.days.from_now
        }
      end
  end
end
add_token(options = {}) click to toggle source
# File lib/acts_as_tokenable/tokenable.rb, line 7
def add_token(options = {})
  options = options.reverse_merge(default_options)

  self.tokens.create(options)
end
default_options() click to toggle source
# File lib/acts_as_tokenable/tokenable.rb, line 28
def default_options
  {
    :expires_at => 30.days.from_now
  }
end