Devise Omniauth Facebook适用于localhost,但不适用于Heroku上的Production

时间:2018-01-21 22:11:13

标签: ruby-on-rails facebook devise omniauth

我有一个使用Heroku的Rails 5.1应用程序。

我正在尝试将Devise与Omniauth Facebook一起使用。问题是我在localhost:3000上完美运行的事实,但是在尝试注册或登录Production时出现错误。

根据Heroku Logs,问题似乎是回调:

2018-01-21T22:00:32.492550+00:00 heroku[router]: at=info method=GET 
path="/auth/auth/facebook/callback?
code=AQC1IE8y6NsiIlFWhCnG_bdH4MoG7XFkOcGRhl4qUAr-
hZ3e6nxTHJR6mothkNKhlFh0NzueLZRaPEtkKBsHb-
PEWNSou5ZAfCwV_M845DT7WKtdwcU6R84c15a0HVys-
9ml0PKI2Wljgu8CzOBz4uhYdPMlkm6AFAvmR1ZCkJ7UGL9Qpm23VQWe-xJ7uv0mtzc9zOIROQT8fQAUW6WUTbHnl-
SywyS0omFU-XiAq2KaXZpcolO7Hnkk0NEgcssZHuBeO6IBZisQchCjPGXi6VdxFmLFgvyxuxrVlSL79ELELqWxXPdpIjn
GVlx2aIpBV12Gqkm8ocI0JhjFbmoD9CUGpd4v4w5kbdlblN3106bZvw&state=ef00f5f6519d9a51e0d9
4846c6c6739bfea76d3e44b3ec69" 
host=www.gourmetcoffee.london request_id=b177986b-
b27c-450e-9950-4af8ef9359af fwd="2a02:c7d:3dc:8e00:91b:3d48:a2e1:4c1d,162.158.154.234" 
dyno=web.1 connect=0ms service=931ms status=500 bytes=1733 protocol=https

我已使用FACEBOOK_APP_IDFACEBOOK_APP_SECRET更新了我的Heroku配置变量。它们也存储在config/application.yml

宝石

#User authentication
gem 'devise'
gem 'omniauth'
gem 'omniauth-facebook', '~>4.0'

的routes.rb

#authentication
  devise_for :users, path: "auth", controllers: {
    sessions: 'users/sessions',
    registrations: 'users/registrations',
    unlocks: 'users/unlocks',
    omniauth_callbacks: 'users/omniauth_callbacks'
  }
  devise_scope :user do
    delete 'sign_out', :to => 'devise/sessions#destroy', :as => :destroy_fb_user_session
  end

omniauth_callbacks_controller.rb

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController

  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"])

  #When a valid user is found, they can be signed in with one of two Devise methods: sign_in or sign_in_and_redirect.

  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

def failure
  redirect_to root_path
end


  protected

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

devise.rb

  config.omniauth :facebook, ENV['FACEBOOK_APP_ID'], ENV['FACEBOOK_APP_SECRET'], scope: "email", info_fields: 'email, first_name, last_name'

有效的OAuth重定向URI

http://localhost:3000/https://www.gourmetcoffee.london

更新 我已修改routes.rb删除path: 'auth'

#authentication
  devise_for :users, controllers: {
    sessions: 'users/sessions',
    registrations: 'users/registrations',
    unlocks: 'users/unlocks',
    omniauth_callbacks: 'users/omniauth_callbacks'
  }

但仍然得到错误。现在似乎是状态= 304,not-modified

2018-01-22T08:21:48.156847+00:00 heroku[router]: at=info method=GET path="/assets/loginWithFacebook" host=www.gourmetcoffee.london request_id=243e2fd1-42c8-4ae1-8bbb-f398e6e9e02c fwd="2a02:c7d:3dc:8e00:a0a1:a430:c07:150,141.101.107.208" dyno=web.1 connect=0ms service=2ms status=304 bytes=322 protocol=https

捆绑exec rake路线

user_facebook_omniauth_authorize GET|POST /users/auth/facebook(.:format)                          users/omniauth_callbacks#passthru
 user_facebook_omniauth_callback GET|POST /users/auth/facebook/callback(.:format)                 users/omniauth_callbacks#facebook

1 个答案:

答案 0 :(得分:0)

我没有使用多个&#34;有效的OAuth重定向URI&#34;喜欢这样。但是,我曾经面临同样的问题。我通过在线域名替换localhost来解决它。让我们再次仔细检查此网址。 替换:

http://localhost:3000/, https://www.gourmetcoffee.london

人:

https://www.gourmetcoffee.london