lib/devise/token_authenticatable/model.rb in devise-token_authenticatable-0.3.0 vs lib/devise/token_authenticatable/model.rb in devise-token_authenticatable-0.3.1

- old
+ new

@@ -1,20 +1,13 @@ module Devise module Models - # The TokenAuthenticatable module is responsible for generating an authentication token and + # The +TokenAuthenticatable+ module is responsible for generating an authentication token and # validating the authenticity of the same while signing in. # # This module only provides a few helpers to help you manage the token, but it is up to you - # to choose how to use it. For example, if you want to have a new token every time the user - # saves his account, you can do the following: + # to choose how to use it. # - # before_save :reset_authentication_token - # - # On the other hand, if you want to generate token unless one exists, you should use instead: - # - # before_save :ensure_authentication_token - # # If you want to delete the token after it is used, you can do so in the # after_token_authentication callback. # # == APIs # @@ -26,19 +19,36 @@ # # def skip_trackable # request.env['devise.skip_trackable'] = true # end # - # == Options - # - # TokenAuthenticatable adds the following options to devise_for: - # - # * +token_authentication_key+: Defines name of the authentication token params key. E.g. /users/sign_in?some_key=... - # module TokenAuthenticatable extend ActiveSupport::Concern + included do + before_save :reset_authentication_token_before_save + before_save :ensure_authentication_token_before_save + end + + module ClassMethods + + def find_for_token_authentication(conditions) + find_for_authentication(authentication_token: conditions[Devise::TokenAuthenticatable.token_authentication_key]) + end + + # Generate a token checking if one does not already exist in the database. + def authentication_token + loop do + token = Devise.friendly_token + break token unless to_adapter.find_first({ authentication_token: token }) + end + end + + Devise::Models.config(self, :expire_auth_token_on_timeout) + + end + def self.required_fields(klass) [:authentication_token] end # Generate new authentication token (a.k.a. "single access token"). @@ -68,23 +78,18 @@ def expire_auth_token_on_timeout self.class.expire_auth_token_on_timeout end - module ClassMethods - def find_for_token_authentication(conditions) - find_for_authentication(authentication_token: conditions[Devise::TokenAuthenticatable.token_authentication_key]) - end + private - # Generate a token checking if one does not already exist in the database. - def authentication_token - loop do - token = Devise.friendly_token - break token unless to_adapter.find_first({ authentication_token: token }) - end + def reset_authentication_token_before_save + reset_authentication_token if Devise::TokenAuthenticatable.should_reset_authentication_token end - Devise::Models.config(self, :token_authentication_key, :expire_auth_token_on_timeout) - end + def ensure_authentication_token_before_save + ensure_authentication_token if Devise::TokenAuthenticatable.should_ensure_authentication_token + end + end end end