简体   繁体   中英

Custom strategy for warden not getting called

I am trying to use a different warden strategy to authenticate my action cable end points. But the strategy is not getting called. I tried to place warden.authenticate:(:action_cable_auth) in a controller to test but none of the debug statements are getting printed on console.

Below are the relevant part of the code.

config/initializers/warden.rb
Warden::Strategies.add(:action_cable_auth)  do
  def valid?
    #check if its a websocket request & for action cable?
    #Rails.logger.error request.inspect
    p 'checking if strategy is valid?'
    true
  end

  def authenticate!
    p 'unauthenticate the user'
    fail!('user not active')
  end
end

in my controller

 warden.authenticate!(:action_cable_auth)

Assuming that you are setting your initializer in the proper place, please recall that if your session is already instantiated somewhere else (for example if you authenticate the user at the point your action is being called, then your strategy will never be called.

This is basically how warden works: if some valid? strategy returns a success! then no other will be called as soon as any authenticate! method in the list of strategies is successful.

Please also be sure that if you want your strategy up the list of strategies to check you may need to also shift it up on the list, such as:

manager.default_strategies(scope: :user).unshift(:action_cable_auth)

Where the manager is your Warden::Manager instance. The scope may also be optional (this is an example where the user scope is used alongside Devise), but you may check your instance .default_strategies to figure out where it is and where you want it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM