Sha256: b9e26e8b3875e0bbfc247ae5b74bcb053ca28b2ba0979c0c99bc78d28f331cea

Contents?: true

Size: 970 Bytes

Versions: 3

Compression:

Stored size: 970 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.exists?(authentication_token: token)
      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

3 entries across 3 versions & 1 rubygems

Version Path
simple_token_authentication-1.5.1 lib/simple_token_authentication/acts_as_token_authenticatable.rb
simple_token_authentication-1.5.0 lib/simple_token_authentication/acts_as_token_authenticatable.rb
simple_token_authentication-1.4.0 lib/simple_token_authentication/acts_as_token_authenticatable.rb