简体   繁体   中英

Devise Omniauth Facebook works on localhost, but not in Production on Heroku

I have a Rails 5.1 app using Heroku.

I am attempting to use Devise with Omniauth Facebook. The problem is the fact I have it working perfectly on localhost:3000, but I get an error when trying to either sign-up or login on Production.

Based on the Heroku Logs, it would appear the issue is with callbacks:

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

I have updated my Heroku config vars with the FACEBOOK_APP_ID and FACEBOOK_APP_SECRET . and they are also stored in config/application.yml

Gems

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

Valid OAuth Redirect URIs

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

UPDATE I have modifed routes.rb removing path: 'auth' :

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

Still getting the error though. Now seems to be a 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

bundle exec rake routes

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

I've not used multiple "Valid OAuth Redirect URIs" like this before. However, I used to face the same problem. I resolved it by replacing localhost by my online domain. Let's double-check this URL again. Replace:

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

By:

https://www.gourmetcoffee.london

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM