设计的自定义认证策略

时间:2010-11-19 07:55:07

标签: ruby-on-rails ruby devise

我需要为https://github.com/plataformatec/devise编写自定义身份验证策略,但似乎没有任何文档。怎么做的?

1 个答案:

答案 0 :(得分:39)

我在设计谷歌小组的this thread中找到了这个非常有用的摘录

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 

将以下内容添加到initializers / devise.rb

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