我正在使用OmniAuth Identity进行正常登录。 Rails 3.2.3和Ruby 1.9.3p194。
目前,当现有用户尝试使用无效凭据登录时,我收到以下错误:
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
我尝试在我的会话结束时添加这样的内容#create action:
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
}
session_controller.rb的失败方法:
def failure
redirect_to login_path, alert: "Authentication failed, please try again."
end
(并且必须重启服务器)
答案 0 :(得分:9)
这似乎是一种有意的开发模式结果。滚动至FAQ。
中的“OmniAuth::FailureEndpoint does not redirect in development mode
”
答案 1 :(得分:0)
常见问题解答中的内容:https://github.com/intridea/omniauth/wiki/FAQ
默认情况下,当身份验证失败时,OmniAuth 1.1.0和更高版本会在开发模式下引发异常。如果您希望它重定向到失败页面,则可以在omniauth配置中包括以下代码:
OmniAuth.config.on_failure = Proc.new { |env|
OmniAuth::FailureEndpoint.new(env).redirect_to_failure
}