2014-03-13 97 views
0

我所做的研究相当数量上我的问题无济于事因此为什么我在计算器:)求助于伟大的人民Facebook登录和CSRF

好吧,我跟着Ryan Bates的在Facebook上登陆教程,但自从教程发布以来,Facebook似乎在两年内发生了很大变化。我一直在努力让Facebook接受localhost:3000 /作为应用程序域。我最终做的是将网站URL和应用程序域设置为我的登台Heroku应用程序以及我放置在localhost中的高级/有效OAuth重定向URI:3000/

好的,问题来了。当我去到localhost:3000/auth /中的Facebook,我得到以下错误:

OmniAuth::Strategies::OAuth2::CallbackError at /auth/facebook/callback 
csrf_detected | CSRF detected 
OmniAuth::FailureEndpoint#raise_out! 
omniauth (1.2.1) lib/omniauth/failure_endpoint.rb, line 25 

然而,当我回到我的网站,我实际上登录奇怪。我还应该补充说我正在使用我的开发者帐户登录。反正这里是我的代码:

sessions_controller.rb

def facebook_login 
    if request.env['omniauth.auth'] 
    user = User.from_omniauth(env['omniauth.auth']) 
    session[:user_id] = user.id 
    redirect_back_or root_path 
    end 
end 

user.rb

class User < ActiveRecord::Base 
    include Tokenable 

    has_many :events 

    has_secure_password 

    validates_presence_of :email, :first_name, :last_name 
    validates_uniqueness_of :email, format: /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i 
    validates_length_of :password, minimum: 6, unless: Proc.new { |a| !a.oauth_token.nil? } 

    def to_param 
    token 
    end 

    def self.from_omniauth(auth) 
    where(auth.slice(:provider, :uid)).first_or_create do |user| 
     user.provider = auth.provider 
     user.uid = auth.uid 
     user.first_name = auth.info.first_name 
     user.last_name = auth.info.last_name 
     user.email = auth.info.email 
     user.password = auth.credentials.token 
     user.password_confirmation = auth.credentials.token 
     user.oauth_token = auth.credentials.token 
     user.oauth_expires_at = Time.at(auth.credentials.expires_at) 
    end 
    end 
end 

的routes.rb

get 'auth/:provider/callback', to: 'sessions#facebook_login' 
    get 'auth/failure', to: redirect('/') 

omniauth.rb

OmniAuth.config.logger = Rails.logger 

Rails.application.config.middleware.use OmniAuth::Builder do 
    provider :facebook, ENV['FACEBOOK_KEY'], ENV['FACEBOOK_SECRET'] 
end 

任何帮助,非常感谢。谢谢!

回答

1

要让Facebook接受本地主机,请使用http://127.0.0.1:3000

扼杀CSRF错误(允许CSRF)在一个特定的控制器,

skip_before_action :verify_authenticity_token! 

这应该可以解决90%的您的烦恼。

+0

这不起作用,不幸的是... – freddyrangel

+0

那里有两个组件,什么没有工作? – OneChillDude

+0

好,所以它实际上在heroku上工作,所以我认为这是最重要的,但它应该在当地工作。第一部分非常出色,谢谢。第二部分不起作用。我试图把它放在应用程序控制器中以实现良好的效果,并且它在那里也不工作。 – freddyrangel