【问题标题】:Devise Omniauth Facebook works on localhost, but not in Production on Heroku设计 Omniauth Facebook 可以在 localhost 上工作,但不能在 Heroku 上生产
【发布时间】:2018-01-21 22:11:13
【问题描述】:

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

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

根据 Heroku 日志,问题似乎与回调有关:

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'
  }

但仍然出现错误。现在好像是status=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

【问题讨论】:

  • 您是否尝试过在生产环境或类似环境中本地运行它?您可以使用 ngrok.io 或 localtunnel.me 之类的东西在本地进行映射。然后,您可以通过将 config.consider_all_requests_local 更改为 true 来进一步显示错误(然后在生产时将其更改回 false)
  • 在我的 facebook 有效 Oauth 重定向 uris 中,我有完整的路径 (whateveryouwant.com/users/auth/facebook/callback)。我还在您的日志中注意到该路径已重复身份验证:“/auth/auth/facebook/callback”
  • @Pablo 我注意到了auth/auth 那个集合在哪里。我一直在寻找它。
  • 我猜 path: 'auth' in your routes 是问题。
  • 谢谢@Pablo 我更新了我的路线,但还是不行。

标签: ruby-on-rails facebook devise omniauth


【解决方案1】:

我以前没有使用过这样的多个“有效 OAuth 重定向 URI”。但是,我曾经面临同样的问题。我通过用我的在线域替换 localhost 来解决它。让我们再次仔细检查这个 URL。 替换:

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

作者:

https://www.gourmetcoffee.london

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-30
    • 1970-01-01
    • 2019-06-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多