2012-09-23 33 views
1

我有一个使用设备的Ruby On Rails 3.x应用程序。如何在登录时添加额外的检查(Rails + Devise)

我的目标是在登录屏幕上添加一个Yubikey输入字段。

我已经产生了意见,调整的画面(即额外字段显示出来),并更新了路线如下:

devise_for :users, :controllers => { :sessions=>"sessions", :registrations => "registrations" }, :path => "users", :path_names => { :sign_in => "login", :sign_out => "logout", :sign_up => "register" } 

不要忘记,我创建了一个会话控制器:

class SessionsController < Devise::SessionsController 
    def create 
    begin 
     if do_some_other_checks 
     super 
     else 
     build_resource 
     clean_up_passwords(resource) 
     flash[:alert] = "Login error" 
     render :new 
     end 
    rescue => e 
     build_resource 
     clean_up_passwords(resource) 
     flash[:alert] = "Login error" 
     render :new 
    end 
    end 
end 

不幸的是,代码并不能正常工作,Devise在用户登录后会调用它,即使附加检查失败,用户仍然登录。

+0

我知道这不完全是你需要但你有看在设计双因素插件? https://github.com/Houdini/two_factor_authentication它可能是值得阅读他的来源,以确保你没有重新发明轮子。 – simonmorley

回答

3

有一个简单的方法来做到这一点在你的用户模型中添加以下内容,不需要创建任何设计控制器或更改默认路由...

class User < ActiveRecord::Base 

# check to see if a user is active or not and deny login if not 
def active_for_authentication? 
    super && do_some_other_checks 
end 

end 
+1

如果您需要使用用户发送的参数,该怎么办? – Altonymous

相关问题