Sha256: 886aeea006eb2d12bb6f485a3833ef907c8604399eef22749ad30d9c140ec7d5

Contents?: true

Size: 974 Bytes

Versions: 5

Compression:

Stored size: 974 Bytes

Contents

module SimpleTokenAuthentication
  module ActsAsTokenAuthenticatable
    extend ActiveSupport::Concern

    # Please see https://gist.github.com/josevalim/fb706b1e933ef01e4fb6
    # before editing this file, the discussion is very interesting.

    included do
      private :generate_authentication_token
    end

    def ensure_authentication_token
      if authentication_token.blank?
        self.authentication_token = generate_authentication_token
      end
    end

    def generate_authentication_token
      loop do
        token = Devise.friendly_token
        break token unless self.class.where(authentication_token: token).first
      end
    end

    module ClassMethods
      def acts_as_token_authenticatable(options = {})
        include SimpleTokenAuthentication::ActsAsTokenAuthenticatable
        before_save :ensure_authentication_token
      end
    end
  end
end
ActiveRecord::Base.send :include, SimpleTokenAuthentication::ActsAsTokenAuthenticatable

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
simple_token_authentication-1.3.0 lib/simple_token_authentication/acts_as_token_authenticatable.rb
simple_token_authentication-1.2.1 lib/simple_token_authentication/acts_as_token_authenticatable.rb
simple_token_authentication-1.2.0 lib/simple_token_authentication/acts_as_token_authenticatable.rb
simple_token_authentication-1.1.1 lib/simple_token_authentication/acts_as_token_authenticatable.rb
simple_token_authentication-1.1.0 lib/simple_token_authentication/acts_as_token_authenticatable.rb