Sha256: 9e5214c14ab0cda7f4d01f05bb4e8a0e3b982e761a841607c5fed3b884bb382c
Contents?: true
Size: 1.27 KB
Versions: 6
Compression:
Stored size: 1.27 KB
Contents
require 'active_support/concern' require 'simple_token_authentication/token_generator' 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 private :token_suitable? private :token_generator end # Set an authentication token if missing # # Because it is intended to be used as a filter, # this method is -and should be kept- idempotent. def ensure_authentication_token if authentication_token.blank? self.authentication_token = generate_authentication_token(token_generator) end end def generate_authentication_token(token_generator) loop do token = token_generator.generate_token break token if token_suitable?(token) end end def token_suitable?(token) self.class.where(authentication_token: token).count == 0 end def token_generator TokenGenerator.instance end module ClassMethods def acts_as_token_authenticatable(options = {}) before_save :ensure_authentication_token end end end end
Version data entries
6 entries across 6 versions & 1 rubygems