2012-07-12 219 views
9

我对正常登录使用OmniAuth身份。 Rails 3.2.3和Ruby 1.9.3p194。为身份登录处理OmniAuth :: Error(invalid_credentials)

目前,当现有用户尝试使用无效的凭证登录,我收到以下错误:

Started POST "/auth/identity/callback" for 127.0.0.1 at 2012-07-12 17:00:03 -0400 
(identity) Callback phase initiated. 
    Identity Load (0.1ms) SELECT "identities".* FROM "identities" WHERE "identities"."email" = 'test' LIMIT 1 
(identity) Authentication failure! invalid_credentials encountered. 

OmniAuth::Error (invalid_credentials): 
    omniauth (1.1.0) lib/omniauth/failure_endpoint.rb:25:in `raise_out!' 
    omniauth (1.1.0) lib/omniauth/failure_endpoint.rb:20:in `call' 
    omniauth (1.1.0) lib/omniauth/failure_endpoint.rb:12:in `call' 
    omniauth (1.1.0) lib/omniauth/strategy.rb:457:in `fail!' 
    omniauth-identity (1.0.0) lib/omniauth/strategies/identity.rb:24:in `callback_phase' 
    omniauth (1.1.0) lib/omniauth/strategy.rb:219:in `callback_call' 
    omniauth (1.1.0) lib/omniauth/strategy.rb:175:in `call!' 
    omniauth (1.1.0) lib/omniauth/strategy.rb:157:in `call' 
    omniauth (1.1.0) lib/omniauth/strategy.rb:177:in `call!' 
    omniauth (1.1.0) lib/omniauth/strategy.rb:157:in `call' 
    omniauth (1.1.0) lib/omniauth/builder.rb:48:in `call' 
    sass (3.1.19) lib/sass/plugin/rack.rb:54:in `call' 
    warden (1.2.1) lib/warden/manager.rb:35:in `block in call' 
    warden (1.2.1) lib/warden/manager.rb:34:in `catch' 
    warden (1.2.1) lib/warden/manager.rb:34:in `call' 
    actionpack (3.2.3) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call' 
    rack (1.4.1) lib/rack/etag.rb:23:in `call' 
    rack (1.4.1) lib/rack/conditionalget.rb:35:in `call' 
    actionpack (3.2.3) lib/action_dispatch/middleware/head.rb:14:in `call' 
    actionpack (3.2.3) lib/action_dispatch/middleware/params_parser.rb:21:in `call' 
    actionpack (3.2.3) lib/action_dispatch/middleware/flash.rb:242:in `call' 
    rack (1.4.1) lib/rack/session/abstract/id.rb:205:in `context' 
    rack (1.4.1) lib/rack/session/abstract/id.rb:200:in `call' 
    actionpack (3.2.3) lib/action_dispatch/middleware/cookies.rb:338:in `call' 
    activerecord (3.2.3) lib/active_record/query_cache.rb:64:in `call' 
    activerecord (3.2.3) lib/active_record/connection_adapters/abstract/connection_pool.rb:467:in `call' 
    actionpack (3.2.3) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call' 
    activesupport (3.2.3) lib/active_support/callbacks.rb:405:in `_run__2431360578939212022__call__3674666166259335855__callbacks' 
    activesupport (3.2.3) lib/active_support/callbacks.rb:405:in `__run_callback' 
    activesupport (3.2.3) lib/active_support/callbacks.rb:385:in `_run_call_callbacks' 
    activesupport (3.2.3) lib/active_support/callbacks.rb:81:in `run_callbacks' 
    actionpack (3.2.3) lib/action_dispatch/middleware/callbacks.rb:27:in `call' 
    actionpack (3.2.3) lib/action_dispatch/middleware/reloader.rb:65:in `call' 
    actionpack (3.2.3) lib/action_dispatch/middleware/remote_ip.rb:31:in `call' 
    actionpack (3.2.3) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call' 
    actionpack (3.2.3) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call' 
    railties (3.2.3) lib/rails/rack/logger.rb:26:in `call_app' 
    railties (3.2.3) lib/rails/rack/logger.rb:16:in `call' 
    quiet_assets (1.0.1) lib/quiet_assets.rb:20:in `call_with_quiet_assets' 
    actionpack (3.2.3) lib/action_dispatch/middleware/request_id.rb:22:in `call' 
    rack (1.4.1) lib/rack/methodoverride.rb:21:in `call' 
    rack (1.4.1) lib/rack/runtime.rb:17:in `call' 
    activesupport (3.2.3) lib/active_support/cache/strategy/local_cache.rb:72:in `call' 
    rack (1.4.1) lib/rack/lock.rb:15:in `call' 
    actionpack (3.2.3) lib/action_dispatch/middleware/static.rb:62:in `call' 
    railties (3.2.3) lib/rails/engine.rb:479:in `call' 
    railties (3.2.3) lib/rails/application.rb:220:in `call' 
    rack (1.4.1) lib/rack/content_length.rb:14:in `call' 
    railties (3.2.3) lib/rails/rack/debugger.rb:20:in `call' 
    railties (3.2.3) lib/rails/rack/log_tailer.rb:14:in `call' 
    rack (1.4.1) lib/rack/handler/webrick.rb:59:in `service' 
    /Users/andrew/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service' 
    /Users/andrew/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run' 
    /Users/andrew/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread' 

我希望能够某种错误处理或抢救添加到该表单,但我不知道如何。这里是形式:

会议/ new.html.haml

%p 
    %strong Don’t use these services? 
    = link_to "Create an account", new_identity_path 
    or login below 

= form_tag "/auth/identity/callback" do 
    .field 
    = label_tag :auth_key, "Email" 
    %br 
     = text_field_tag :auth_key 
    .field 
    = label_tag :password 
    %br 
     = password_field_tag :password 

我的会话控制器:

sessions_controller.rb

class SessionsController < ApplicationController 

    def create 
    debugger 
    user = User.from_omniauth(env["omniauth.auth"]) 
    puts "ENVIRONMENT VARIABLE: #{env["omniauth.auth"].inspect}" 
    puts "This is the user id: #{user.id}" 

    session[:user_id] = user.id 
    redirect_to root_url, notice: "Signed in!" 

    end 

    def destroy 
    session[:user_id] = nil 
    redirect_to root_url, notice: "Signed out!" 
    end 

    def failure 
    redirect_to signup_path, alert: "Authentication failed, please try again." 
    end 

end 
    .actions= submit_tag "Login" 

我的用户模型:

user.rb

class User < ActiveRecord::Base 
    attr_accessible :email, :name, :provider, :uid 

    validates_presence_of :email 
    validates_uniqueness_of :email 
    validates_format_of :email, :with => /^[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}$/i 

    def self.from_omniauth(auth) 
    where(auth.slice("provider", "uid")).first || create_from_omniauth(auth) 
    end 

    def self.create_from_omniauth(auth) 

    puts "Auth object: #{auth.inspect}" 
    create! do |user| 
     user.provider = auth["provider"] 
     user.uid = auth["uid"] 
     user.name = auth["info"]["name"] 
     user.email = auth["info"]["email"] 
     user.admin = false 
    end 


    end 

end 

我OmniAuth配置文件:

omniauth.rb

OmniAuth.config.logger = Rails.logger 

Rails.application.config.middleware.use OmniAuth::Builder do 
    provider :github, ENV['6c268d450fb1149afc3d'], ENV['1dba63b45a0c8b7e62dea9fc5c9a8f69bf6142b1'] 
    provider :identity, on_failed_registration: lambda { |env| 
    IdentitiesController.action(:new).call(env) 
    } 
end 

我尝试添加这样的事情对我的会话结束#创建行动:

rescue OmniAuth::Error 
    flash[:error] = "invalid credentials" 
    redirect_to signup_path 

但我仍然在无效登录时收到了原始错误消息。我能做些什么来处理无效的OmniAuth身份登录?

更新:

解决的办法是添加以下代码的omniauth.rb:

OmniAuth.config.on_failure = Proc.new { |env| 
    OmniAuth::FailureEndpoint.new(env).redirect_to_failure 
} 

,未能方法sessions_controller.rb:

def failure 
    redirect_to login_path, alert: "Authentication failed, please try again." 
end 

(和强制服务器重启)

+0

还必须将'get'/ auth/failure'添加到'sessions#failure''到routes.rb – 2016-08-18 14:39:10

回答

9

这似乎是一种有意的开发模式结果。滚动到FAQ中的“OmniAuth::FailureEndpoint does not redirect in development mode”。

+0

即使有效的用户登录(开发模式),我也会收到此错误,我该如何解决这一个 – Ross 2012-10-16 05:18:37

+0

@Ross:你没有一个有效的用户登录的可能性很大。将你正在做什么,看到什么以及期望看到的细节放在一起,然后提交一个新问题。这听起来完全不同于这个特定的问题。 – jdl 2012-10-16 11:59:00

+1

检查了这一点,如果你可以帮助http://stackoverflow.com/questions/12908608/connection-failed-for-google-open-id-using-omniauth – Ross 2012-10-16 12:09:09