1

Im using devise omniauthable for user authentication both with google and facebook. Google works just fine but facebook gets stucked on a redirect page, although on their platform the user logs in correctly (on facebook).

This is the log i get:

2014-09-01T15:26:41.996884+00:00 app[web.1]: (facebook) Request phase initiated.
2014-09-01T15:26:42.211524+00:00 app[web.1]: (facebook) Request phase initiated.
2014-09-01T15:26:41.994627+00:00 app[web.1]: Started GET "/users/auth/facebook?locale=es" for 190.15.201.45 at 2014-09-01 15:26:41 +0000
2014-09-01T15:26:42.205674+00:00 app[web.1]: Started GET "/users/auth/facebook?locale=es" for 190.15.201.45 at 2014-09-01 15:26:42 +0000
2014-09-01T15:26:42.217355+00:00 heroku[router]: at=info method=GET path="/users/auth/facebook?locale=es" host=myapp.herokuapp.com request_id=2b9aab45-c511-4ac9-b36f-4e6925cba3aa fwd="190.15.201.45" dyno=web.1 connect=2ms service=16ms status=302 bytes=1284

My routes:

devise_for :users, :controllers => { omniauth_callbacks: "users/omniauth_callbacks" }

My omniauth_callbacks_controller:

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController

def self.provides_callback_for(provider)
class_eval %Q{
  def #{provider}
    @user = User.find_for_oauth(env["omniauth.auth"], current_user)

    if @user.persisted?
      sign_in_and_redirect @user, event: :authentication
      set_flash_message(:notice, :success, kind: "#{provider}".capitalize) if is_navigational_format?
    else
      session["devise.#{provider}_data"] = env["omniauth.auth"]
      redirect_to new_user_registration_url
    end
  end
}
end

[:google_oauth2, :facebook].each do |provider|
   provides_callback_for provider
end

My user model:

class User < ActiveRecord::Base

devise :database_authenticatable, :confirmable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable, :omniauthable

belongs_to :company

before_save :create_user_company

def create_user_company
  if self.company_id.nil?
    company = Company.new
    company.email = self.email
    company.save
    self.company_id = company.id
    self.admin = true
  end
end

def self.find_for_oauth(auth, signed_in_resource = nil)
  identity = Identity.find_for_oauth(auth)
  user = signed_in_resource ? signed_in_resource : identity.user

  if user.nil?
    email = auth.info.email
    user = User.where(:email => email).first if email

    if user.nil?
      user = User.new(
        name: auth.extra.raw_info.name,
        email: email ? email : "temp_email@mail.com",
        password: Devise.friendly_token[0,20]
      )
      user.skip_confirmation!
      user.save!
    end
  end

  if identity.user != user
    identity.user = user
    identity.save!
  end

  user

end

end

Devise initializer is the basic:

config.omniauth :facebook, ENV['FB_APP_ID'], ENV['FB_APP_SECRET']

But i also tried this (didnt work):

config.omniauth :facebook, ENV['FB_APP_ID'], ENV['FB_APP_SECRET'],{client_options: {ssl: {ca_file: Rails.root.join('lib/assets/cacert.pem').to_s}}}

And im using this gems:

gem 'omniauth'
gem 'omniauth-google-oauth2'
gem 'omniauth-facebook'
gem 'devise'
gem 'figaro' #for safe saving of env vars

I would appreciate any hint you might have. Thanks in advance.

PS: this is not the answer

Community
  • 1
  • 1
ntonnelier
  • 1,539
  • 3
  • 23
  • 49
  • Something to take in consideration is it never gets to "method=GET path="/users/auth/facebook/callback". Only to "method=GET path="/users/auth/facebook" and then it stucks. – ntonnelier Sep 01 '14 at 20:10
  • + on the browsers Network console i get a "500 internal server error" – ntonnelier Sep 01 '14 at 20:23

1 Answers1

0

Ok, if anyone encounters the same problem, this fixed it:

  • Go into your facebook developer console (developers.facebook)
  • Enter your App:
    1. On Setting you need to add a contact email.
    2. On Status and Reviews you will find this question (former Sandbox): "Do you want to make this app and all its live features available to the general public?". Click "Yes".
ntonnelier
  • 1,539
  • 3
  • 23
  • 49