2016-12-25 81 views
0

我正在设置登录/注册与Facebook(设计),但作为其他人,我没有运气,使其工作。我已经阅读了很多来自其他人的帖子和文章,并且经历了几次设置,但仍然是相同的结果。未找到。身份验证通过。 (设计oauth脸谱)

当我点击“使用Facebook登录”按钮时,我收到消息“Not found。Authentication passthru”。

它从来没有去通过“脸谱”控制器的行动,但总是通过“passthru”行动。

我用: - 耙V2.3.0 - 轨v4.2.3 - 制定V3.5.2

感谢您的任何帮助。 的Miroslav


omniauth_callbacks_controller.rb
class OmniauthCallbacksController < Devise::OmniauthCallbacksController 
    # You should configure your model like this: 

    # You should also create an action method in this controller like this: 
    def facebook 
    # You need to implement the method below in your model (e.g. app/models/user.rb) 
    @user = User.from_omniauth(request.env["omniauth.auth"]) 

    if @user.persisted? 
     sign_in_and_redirect @user, :event => :authentication #this will throw if @user is not activated 
     set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format? 
    else 
     session["devise.facebook_data"] = request.env["omniauth.auth"] 
     redirect_to new_user_registration_url 
    end 
    end 

    # More info at: 
    # https://github.com/plataformatec/devise#omniauth 

    # GET|POST /resource/auth/facebook 
    def passthru 
    super 
    end 

    # GET|POST /users/auth/facebook/callback 
    def failure 
    super 
    #redirect_to root_path 
    end 

    # protected 

    # The path used when OmniAuth fails 
    def after_omniauth_failure_path_for(scope) 
    super(scope) 
    end 

    #def failure 
    # redirect_to root_path 
    #end 

end 

User.rb
class User < ActiveRecord::Base 
    devise :database_authenticatable, :registerable, :confirmable, 
    :recoverable, :rememberable, :trackable, :omniauthable, :validatable, password_length: 6..72, :omniauth_providers => [:facebook] 

    def self.from_omniauth(auth) 
    where(provider: auth.provider, uid: auth.uid).first_or_create do |user| 
     user.email = auth.info.email 
     user.password = Devise.friendly_token[0,20] 
     user.rolable.firstname = auth.info.firstname # assuming the user model has a name 
     user.rolable.lastname = auth.info.lastname 
     user.image = auth.info.image # assuming the user model has an image 
     # If you are using confirmable and the provider(s) you use validate emails, 
     # uncomment the line below to skip the confirmation emails. 
     # user.skip_confirmation! 
    end 
    end 

end 

devise.rb
Devise.setup do |config| 
    config.mailer = 'CustomDeviseMailer' 

    require 'devise/orm/active_record' 

    config.case_insensitive_keys = [:email] 
    config.strip_whitespace_keys = [:email] 
    config.http_authenticatable_on_xhr = false 
    config.skip_session_storage = [:http_auth] 
    config.stretches = Rails.env.test? ? 1 : 10 
    config.allow_unconfirmed_access_for = 1.days 
    config.reconfirmable = false 
    config.expire_all_remember_me_on_sign_out = true 
    config.password_length = 6..72 
    config.reset_password_within = 24.hours 
    config.sign_out_via = :delete 
    config.omniauth :facebook, ENV['FACEBOOK_APP_ID'], ENV['FACEBOOK_APP_SECRET'] 

end 

routes.rb中 条
devise_for :users, path: "auth", :controllers => {sessions: 'sessions', registrations: 'registrations', confirmations: 'confirmations', passwords: 'passwords', omniauth_callbacks: 'omniauth_callbacks'} 

耙路线

user_omniauth_authorize_en GET|POST /en/auth/auth/:provider(.:format)                 omniauth_callbacks#passthru {:locale=>"en", :provider=>/facebook/} 
user_omniauth_authorize_ma GET|POST /ma/auth/auth/:provider(.:format)                 omniauth_callbacks#passthru {:locale=>"ma", :provider=>/facebook/} 
user_omniauth_authorize_cs GET|POST /cs/auth/auth/:provider(.:format)                 omniauth_callbacks#passthru {:locale=>"cs", :provider=>/facebook/} 
user_omniauth_callback_en GET|POST /en/auth/auth/:action/callback(.:format)               omniauth_callbacks#(?-mix:facebook) {:locale=>"en"} 
user_omniauth_callback_ma GET|POST /ma/auth/auth/:action/callback(.:format)               omniauth_callbacks#(?-mix:facebook) {:locale=>"ma"} 
user_omniauth_callback_cs GET|POST /cs/auth/auth/:action/callback(.:format)               omniauth_callbacks#(?-mix:facebook) {:locale=>"cs"} 

login.html.erb

<%= button_to t(:login_form_link_facebook), user_omniauth_authorize_path(:facebook), :title => t(:login_form_link_facebook), :class => "btn btn-fcb full-w capital semi-margin-bottom bold", :method => :get %> 
+0

当我删除行 “config.omniauth:脸谱,ENV [ 'FACEBOOK_APP_ID'],ENV [ 'FACEBOOK_APP_SECRET']” 从devise.rb,它不起作用。相同的行为,所以我认为配置有问题? – Miroslav

回答

0

我的问题是与路线。我使用的是本地化的网址,它导致了facebook omniauth的问题。

代替:

localized do 
    devise_for :users, path: "auth", :controllers => {sessions: 'sessions', registrations: 'registrations', confirmations: 'confirmations', passwords: 'passwords', omniauth_callbacks: 'omniauth_callbacks'} 
end 

我使用:

devise_for :users, path: "auth", :controllers => {sessions: 'sessions', registrations: 'registrations', confirmations: 'confirmations', passwords: 'passwords', omniauth_callbacks: 'omniauth_callbacks'}