簡體   English   中英

omn​​iauth的設計和路線

[英]Devise and routes for omniauth

我正在使用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]

我在這里想念什么?

謝謝

在所有引用的地方驗證提供者的拼寫是否一致。
以下數據是: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,因為devise是omniauthable的(我們在內部初始化時將devise.rb代替omniauth.rb用作中間件配置)。 參考:以下段落, 在您開始 Devise Overview Wiki之前https//github.com/plataformatec/devise/wiki/OmniAuth%3A-Overview

還是沒有愛? 然后,我將檢查omniauth- provider寶石以了解正在使用的任何細節。 Facebook在2015年7月至2015年8月期間更改了OAuth API,因此需要在配置中的最后一部分(如上所示,Devise Config,devise.rb)來獲取用戶的電子郵件地址和名稱:

  info_fields: 'email, name'

做這個

<% 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 %>

omn​​iauth路徑采用provider選項。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM