gpt4 book ai didi

ruby-on-rails - Devise omniauthable 使用 `Could not find a valid mapping for path` 破坏 Omniauth 身份验证

转载 作者:数据小太阳 更新时间:2023-10-29 08:10:18 36 4
gpt4 key购买 nike

在我的项目中,我有两类用户:求职者和招聘经理。求职者没有模型,他们只能使用从第三方供应商处收到的数据申请工作,同时通过 Omniauth 进行身份验证。招聘经理的信息存储在设计用户模型中。招聘经理还必须能够使用其公司的 Google 电子邮件帐户登录。因此,首先我使用 Omniauth 1.0.0、Rails 3.1.3 构建求职者身份验证:

omniauth.rb

require 'omniauth-openid'
require 'openid/store/filesystem'
Rails.application.config.middleware.use OmniAuth::Builder do
provider :openid, :store => OpenID::Store::Filesystem.new('./tmp'), :name => 'google', :identifier => 'https://www.google.com/accounts/o8/id'
provider :facebook, "xxxxxxxxx", "xxxxxxxxxxxxxxxxxxxxxxxxxxxx",
{:scope => 'email, offline_access, publish_stream', :client_options => {:ssl => {:ca_file => '/usr/lib/ssl/certs/ca-certificates.crt'}}}
provider :twitter, "xxxxxxxxxxx", "xxxxxxxxxxxxxxxxxxxxxxxxxxx"
provider :linkedin, "xxxxxxxxxxx", "xxxxxxxxxxxxxxxxxxx"
end

routes.rb 中:

match '/auth/:provider/callback', :to => 'sessions#authenticate_jobseeker'
match '/auth/failure', :to => 'sessions#failure'

sessions_controller.rb

def authenticate_jobseeker
session[:jobseeker] = request.env['omniauth.auth']

if valid_job_seeker?
redirect_to new_job_application_path(...)
else
redirect_to request.env['omniauth.origin'] || root_path, alert: "Authentication failure"
end
end

到目前为止,一切正常。但是,当我开始为用户模型实现 Google 登录并向其添加 :omniauthable 时,我的求职者身份验证失败了。我正在使用 Devise 1.5.2:

user.rb

class User < ActiveRecord::Base
#...
devise :database_authenticatable, :registerable,
... :lockable, :omniauthable
#...
end

devise.rb 中:

config.omniauth :open_id, :store => OpenID::Store::Filesystem.new('./tmp'), :name => 'google', :identifier => 'https://www.google.com/accounts/o8/id', :require => 'omniauth-openid'

routes.rb 中:

devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" } do
get '/users/auth/:provider' => 'users/omniauth_callbacks#passthru'
end

此时,用户的身份验证有效,但求职者的身份验证无效。搜索了一段时间后,通过向 omniauth.rb 中的每个提供程序添加 :path_prefix => "/auth" 解决了这个问题。现在唯一的问题是,当求职者不允许访问其数据(即按“不允许”并返回到应用程序)时,我会为每个提供者跟踪 RuntimeError:

Could not find a valid mapping for path "/auth/twitter/callback" 
Parameters:
{"denied"=>"mKjVfMRwRAN12ZxQ9cxCoD4rYSLJIRLnEqgiI"}

跟踪的顶部:

devise (1.5.2) lib/devise/mapping.rb:48:in `find_by_path!'
devise (1.5.2) lib/devise/omniauth.rb:17:in `block in <top (required)>'
omniauth (1.0.0) lib/omniauth/strategy.rb:418:in `call'
omniauth (1.0.0) lib/omniauth/strategy.rb:418:in `fail!'
omniauth-oauth (1.0.0) lib/omniauth/strategies/oauth.rb:63:in `rescue in callback_phase'
omniauth-oauth (1.0.0) lib/omniauth/strategies/oauth.rb:45:in `callback_phase'
omniauth (1.0.0) lib/omniauth/strategy.rb:200:in `callback_call'
omniauth (1.0.0) lib/omniauth/strategy.rb:166:in `call!'
omniauth (1.0.0) lib/omniauth/strategy.rb:148:in `call'
omniauth (1.0.0) lib/omniauth/strategy.rb:168:in `call!'
omniauth (1.0.0) lib/omniauth/strategy.rb:148:in `call'
omniauth (1.0.0) lib/omniauth/strategy.rb:168:in `call!'
omniauth (1.0.0) lib/omniauth/strategy.rb:148:in `call'
omniauth (1.0.0) lib/omniauth/builder.rb:30:in `call'

我一直在努力解决这个问题。任何帮助是极大的赞赏。如果我可以提供更多信息,请告诉我。

最佳答案

回答我自己的问题。因此,最终决定采用纯粹的 Omniauth 实现。我从 User 模型中删除了 :omniauthable,从 devise.rb 中删除了 config.omniauth...,删除了 :omniauth_callbacksroutes.rb 设计路由。因此,所有用户(无论什么角色)都将使用相同的回调路由并点击 sessions_controller#authenticate_jobseeker 操作(应该考虑重命名操作?):

def authenticate_jobseeker
auth_hash = request.env['omniauth.auth']

unless auth_hash.present?
redirect_to request.env['omniauth.origin'] || root_path, alert: "Sorry, we were not able to authenticate you" and return
end

@user = User.find_from_oauth(auth_hash)
if @user.present?
flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Google"
sign_in_and_redirect @user, :event => :authentication and return
else
session[:jobseeker] = auth_hash["info"]
if valid_job_seeker?
redirect_to new_job_application_path(...)
end
end
end

User.find_from_oauth:

def self.find_from_oauth(auth_hash)
if auth_hash
user = User.where(:email => auth_hash["info"]["email"]).first
end
user
end

此实现满足所有要求。

关于ruby-on-rails - Devise omniauthable 使用 `Could not find a valid mapping for path` 破坏 Omniauth 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8577089/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com