开发者

Custom authentication strategy for devise

开发者 https://www.devze.com 2023-01-25 17:27 出处:网络
I nee开发者_如何学运维d to write a custom authentication strategy for https://github.com/plataformatec/devise but there doesn\'t seem to be any docs. How\'s it done? I found this very helpful snippet

I nee开发者_如何学运维d to write a custom authentication strategy for https://github.com/plataformatec/devise but there doesn't seem to be any docs. How's it done?


I found this very helpful snippet in this thread on the devise google group

initializers/some_initializer.rb:

Warden::Strategies.add(:custom_strategy_name) do 
  def valid? 
    # code here to check whether to try and authenticate using this strategy; 
    return true/false 
  end 

  def authenticate! 
    # code here for doing authentication; 
    # if successful, call  
    success!(resource) # where resource is the whatever you've authenticated, e.g. user;
    # if fail, call 
    fail!(message) # where message is the failure message 
  end 
end 

add following to initializers/devise.rb

  config.warden do |manager| 
     manager.default_strategies.unshift :custom_strategy_name 
  end 
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号