The slainer68 approach is good, but it does not work for me as it is, so I will add my possible solution here.
In config/initializers/devise.rb , which already had a Devise.setup do |config| ... end block Devise.setup do |config| ... end Devise.setup do |config| ... end , I added the following:
Warden::Strategies.add(:my_token_authenticatable, Devise::Strategies::TokenAuth def valid? mapping.to.respond_to?(:authenticate_with_token) && authentication_token(scope).present? && params[:controller] == 'photos' && params[:action] == 'create' end end
I also added this to the Devise.setup block:
config.warden do |manager| manager.default_strategies.unshift :my_token_authenticatable end
It would be better to simply update the existing strategy :token_authenticatable , but it was not loaded into Warden by the time this code was executed. As a result of using a strategy with a different name, I had to duplicate some of the methods and methods of the class from Devise::Strategies::TokenAuthenticatable , including:
reset_authentication_tokenreset_authentication_token!self.authenticate_with_token(attributes)self.token_authentication_keyvalid_authentication_token?(incoming_auth_token)self.find_for_token_authentication(token)
I also had to remove: token_authenticatable from the devise line at the top of the user model.
I also made a slainer68 decision and added :stateless_token => false to the devise_for parameters in config/routes.rb .
source share