Search code examples
ruby-on-railsdeviseomniauthomniauth-facebook

sign in via facebook redirects to registration path


I have problem sign in via facebook I use mongoid 4.0.0, rails 4.1.5, devise 3.3.0 and omniauth-facebook 2.0.0

And I use this example https://github.com/plataformatec/devise/wiki/OmniAuth%3A-Overview

If I try to sign in via facebook it redirects me to the registration page

http://localhost:3000/users/registration#_=_

log file

Started GET "/users/auth/facebook" for 127.0.0.1 at 2014-09-28 12:48:22 +0400
I, [2014-09-28T12:48:22.189885 #1102]  INFO -- omniauth: (facebook) Request phase initiated.


Started GET "/users/auth/facebook/callback?code=AQAM69gxg_GR3sGFmIM16U2AfNUM-UdImifF2qNbJz0xiiWVRMCjc_gFSncvkeeSDbiv0pMwLkS7zG5HQoEHmlG7e1tnZYcnqYMEh_YEsstNucLTWQRYcjIpsHCzymDrW4u7xYm-YtRvghLABMmlExkAsJ4MZ4AJ2aI6bMWHF_mG9d1hlZ5x2MnsbJGDXNp62qQh2U7j6njD3spLPco22s4LVRPsehbI4o7JlY58KwGaAmzXM_FKbA_nERI_1JCt56x8PFQvE4c4d75ABjgkYyoIj4DTXdjRp-nJ1JvlK_FZWT6kqTONcw_KaT2B0iXujogB-QP2SikXwgN_N0e_1GhW&state=4e58d1a68766eab76419e283e89a9f5ad6d8fa6f3c0ab870" for 127.0.0.1 at 2014-09-28 12:48:22 +0400
I, [2014-09-28T12:48:22.506731 #1102]  INFO -- omniauth: (facebook) Callback phase initiated.
Processing by Users::OmniauthCallbacksController#facebook as HTML
  Parameters: {"code"=>"AQAM69gxg_GR3sGFmIM16U2AfNUM-UdImifF2qNbJz0xiiWVRMCjc_gFSncvkeeSDbiv0pMwLkS7zG5HQoEHmlG7e1tnZYcnqYMEh_YEsstNucLTWQRYcjIpsHCzymDrW4u7xYm-YtRvghLABMmlExkAsJ4MZ4AJ2aI6bMWHF_mG9d1hlZ5x2MnsbJGDXNp62qQh2U7j6njD3spLPco22s4LVRPsehbI4o7JlY58KwGaAmzXM_FKbA_nERI_1JCt56x8PFQvE4c4d75ABjgkYyoIj4DTXdjRp-nJ1JvlK_FZWT6kqTONcw_KaT2B0iXujogB-QP2SikXwgN_N0e_1GhW", "state"=>"4e58d1a68766eab76419e283e89a9f5ad6d8fa6f3c0ab870"}
  MOPED: 54.196.12.51:10063 COMMAND      database=admin command={:ismaster=>1} runtime: 146.1740ms
  MOPED: 54.196.12.51:10063 QUERY        database=arch_dev collection=users selector={"$query"=>{"provider"=>"facebook", "uid"=>"736207756417197"}, "$orderby"=>{"created_at"=>-1}} flags=[] limit=-1 skip=0 batch_size=nil fields=nil runtime: 140.1610ms
Redirected to http://localhost:3000/users/registration
Completed 302 Found in 1888ms

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

    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
end

user.rb

  def self.from_omniauth(auth)
    where(auth.slice(:provider, :uid)).first_or_create do |user|
      user.email = auth.info.email
      user.password = Devise.friendly_token[0,20]
      user.name = auth.info.name
      user.username = auth.info.last_name
      user.avatar_url = auth.info.image
    end
  end

Solution

  • The problem was in the Facebook permissions

    Users without friends can't register via Facebook