Rana Umer
Rana Umer

Reputation: 11

Invalid App ID facebook, Omniauth rails 5.2.4

When I sign in my app with facebook I am getting the error "Invalid App ID" even I verified my app id and it is correct.

what am I doing wrong in my code?

Here is my code:

devise.rb

config.omniauth :facebook, 'xxxxxxxxxxxx', 'xxxxxxxxxxxxxxxxxxxxx', token_params: { parse: :json }

omniauth.rb

Rails.application.config.middleware.use OmniAuth::Builder do
    provider :developer unless Rails.env.production?
    provider :facebook, ENV['FACEBOOK_APP_ID'], ENV['FACEBOOK_APP_SECRET']
end

routes.rb

Rails.application.routes.draw do

root to: 'surveys#index'
resources :surveys

devise_for :users, controllers: {
    sessions: 'users/sessions', 
    confirmations: 'confirmations',
    omniauth_callbacks: "users/omniauth_callbacks"
}
end

omniauth_callbacks_controller.rb

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController

  def facebook
    @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


  def failure
    redirect_to root_path
  end
end

user.rb

class User < ApplicationRecord
    has_many :surveys
    mount_uploader :avatar, AvatarUploader

    devise :database_authenticatable, :registerable, 
    :recoverable, :rememberable, :validatable, :confirmable,
    :omniauthable, omniauth_providers: %i[facebook]

    def self.from_omniauth(auth)
      where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
        user.email = auth.info.email
        user.password = Devise.friendly_token[0, 20]
        user.first_name = auth.info.first_name   # assuming the user model has a name
        # If you are using confirmable and the provider(s) you use validate emails, 
        # uncomment the line below to skip the confirmation emails.
        user.skip_confirmation!
      end
    end

    def self.new_with_session(params, session)
        super.tap do |user|
          if data = session["devise.facebook_data"] && session["devise.facebook_data"]["extra"]["raw_info"]
            user.email = data["email"] if user.email.blank?
          end
        end
    end
end

Thanks in advance

Upvotes: 1

Views: 272

Answers (1)

Tun
Tun

Reputation: 1447

I think you are configuring omniauth two times. You should remove omniauth.rb under config/initializers folder.

Remember that config.omniauth adds omniauth provider middleware to your application. This means you should not add this provider middleware again in config/initializers/omniauth.rb as they'll clash with each other and result in always-failing authentication.

Check Devise wiki on Facebook omniauth for Facebook integration.

Upvotes: 1

Related Questions