【问题标题】:Devise and routes for omniauth为omniauth设计和路由
【发布时间】:2014-06-08 20:16:20
【问题描述】:

我正在使用 Devise 对用户进行身份验证,这就是 routes.rb 文件中的内容:

devise_for :users, :path_names => { :sign_in => 'login', :sign_out => 'logout', :password => 'secret',
                                    :confirmation => 'verification', :unlock => 'unlock', :registration => 'register',
                                    :sign_up => 'signup' }

我为omniauth(LinkedIn)添加了一部分:

devise_for :users, :path_names => { :sign_in => 'login', :sign_out => 'logout', :password => 'secret',
                                    :confirmation => 'verification', :unlock => 'unlock', :registration => 'register',
                                    :sign_up => 'signup' }, :controllers => { :omniauth_callbacks => "omniauth_callbacks" }

在视图中:

<% if user_signed_in? %>
  Signed in as <%= current_user.email %>. Not you?
  <%= link_to "Sign out", destroy_user_session_path,:method => :delete %>
  <% else %>
  <%= link_to "Sign up", new_user_registration_path %> or
  <%= link_to "Sign in", new_user_session_path %>
  <%= link_to "Sign in with Linkedin",user_omniauth_authorize_path(:linkedin) %>
<% end %>

还有错误:

No route matches {:controller=>"omniauth_callbacks", :action=>"passthru", :provider=>:linkedin, :format=>nil} missing required keys: [:provider]

我在这里错过了什么?

谢谢

【问题讨论】:

  • 您使用的omniauth gem 的确切名称是什么?
  • gem 'omniauth'gem 'omniauth-linkedin'
  • 您是否使用 twitter omniauth 更新了模型并设计了配置?这篇文章还不错sourcey.com/…

标签: ruby-on-rails ruby devise linkedin omniauth


【解决方案1】:

在所有引用的地方验证您的提供者的拼写是否一致。
以下数据是 :facebook 作为提供者的示例。

具有设计全能调用的模型文件

# app/models/User.rb
  devise :omniauthable, :omniauth_providers => [:facebook]

为提供者设计配置

# app/config/initializers/devise.rb
  config.omniauth :facebook, ENV['FACEBOOK_APP_ID'], ENV['FACEBOOK_APP_SECRET'], scope: 'email', info_fields: 'email, name'

^-- 另外,向提供商注册以获得您的开发者APP_ID和APP_SECRET

回调控制器:

# app/controllers/callbacks_controller.rb
  def facebook
    @user = User.from_omniauth(request.env["omniauth.auth"])
    ... more code here, excess omitted ...
  end

视图文件中的 URI 路径

# apps/views/shared/_footer.html.erb
  user_omniauth_authorize_path(:facebook)

在这种情况下,我只使用提供者特定的 gem:

# app_name/Gemfile
  gem 'omniauth-facebook'

我没有使用#omniauth gem,因为设计是omniauthable(我们在初始化中使用devise.rb 而不是omniauth.rb 作为中间件配置)。参考:Devise Overview wiki开始之前下面的段落:https://github.com/plataformatec/devise/wiki/OmniAuth%3A-Overview

还没有爱吗?然后我会检查 omniauth-provider gem 以了解使用中的任何细节。 Facebook 在 2015 年 7 月至 8 月左右更改了他们的 OAuth API,因此需要配置中的最后一部分(如上图所示,Devise Config,devise.rb)来获取用户的电子邮件地址和姓名:

  info_fields: 'email, name'

【讨论】:

  • 请阅读meta.stackexchange.com/questions/22186/…,它解释了如何正确格式化代码块。谢谢。
  • @MathiasMüller ,我编辑了答案。如果它仍然关闭,请告诉我我在格式中遗漏了什么。谢谢。
  • 现在格式完美了,感谢改进。
【解决方案2】:

这样做

<% if user_signed_in? %>
  Signed in as <%= current_user.email %>. Not you?
  <%= link_to "Sign out", destroy_user_session_path,:method => :delete %>
  <% else %>
  <%= link_to "Sign up", new_user_registration_path %> or
  <%= link_to "Sign in", new_user_session_path %>
  <%= link_to "Sign in with Linkedin",user_omniauth_authorize_path(provider: :linkedin) %>
<% end %>

omniauth 路径采用provider 选项。

【讨论】:

  • 感谢帕里托什的回答。不过,我仍然面临这个问题:No route matches {:controller=&gt;"omniauth_callbacks", :action=&gt;"passthru", :provider=&gt;"linkedin"} missing required keys: [:provider]
  • 请发布rake routes | grep omniauth的输出
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-18
  • 1970-01-01
  • 1970-01-01
  • 2011-09-30
相关资源
最近更新 更多