mardi 19 septembre 2017

OmniAuth-Facecbook Rails Gem Error

I have this problem with the omniauth-facebook gem the error is

Not found. Authentication passthru.

and here's the logs.

Started GET "/users/auth/facebook" for 127.0.0.1 at 2017-09-19 21:33:14 +0800 Processing by Users::OmniauthCallbacksController#passthru as HTML Rendering text template Rendered text template (0.9ms) Completed 404 Not Found in 14ms (Views: 4.5ms | ActiveRecord: 0.0ms)

here are my gemfiles

gem 'devise'
gem 'omniauth'
gem 'omniauth-facebook'
gem 'sprockets-rails', '~> 3.1', '>= 3.1.1'

My User.rb files

class User < ApplicationRecord
  extend Devise::Models
  acts_as_voter
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable,
         :omniauthable, :omniauth_providers => [:facebook]

  has_one :profile, dependent: :destroy

  has_many :posts, dependent: :destroy # remove a user's posts if his account is deleted.
  has_many :active_relationships, class_name: "Relationship", foreign_key: "follower_id", dependent: :destroy
  has_many :passive_relationships, class_name: "Relationship", foreign_key: "followed_id", dependent: :destroy

  has_many :following, through: :active_relationships, source: :followed
  has_many :followers, through: :passive_relationships, source: :follower

  has_many :items, dependent: :destroy

  def self.find_for_oauth(auth)
    user = User.where(uid: auth.uid, provider: auth.provider).first

    unless user
      user = User.create(
        uid:      auth.uid,
        provider: auth.provider,
        email:    User.dummy_email(auth),
        password: Devise.friendly_token[0, 20]
      )
    end

    user
  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

  private

  def self.dummy_email(auth)
    "#{auth.uid}-#{auth.provider}@example.com"
  end
end

my devise.rb files

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

My OmniauthController files

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
  def facebook
    callback_from :facebook
  end

  # def twitter
  #   callback_from :twitter
  # end

  private

  def callback_from(provider)
    provider = provider.to_s

    @user = User.find_for_oauth(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 => provider.capitalize) if is_navigational_format?
    else
      session["devise.#{provider}_data"] = request.env["omniauth.auth"]
      redirect_to new_user_registration_url
    end
  end
end

Aucun commentaire:

Enregistrer un commentaire