mrabro
mrabro

Reputation: 1097

omniauth-facebook request.env['omniauth.auth'] is nil [DUPLICATED]

I have been looking for the solution but got no any luck, have searched alot and tried many solutions found on stackoverflow or github issues... but still same error..

Referring to this post I tried, but still no luck..

here is the code I have in my app.

The Gem File

gem 'omniauth', '2.0.4'
gem 'omniauth-oauth2', '1.7.2'
gem 'omniauth-facebook', '9.0'
gem 'omniauth-rails_csrf_protection', '~> 1.0'

User Model:

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

def self.from_omniauth(auth)
    name_split = auth.info.name.split(" ")
    user = User.where(email: auth.info.email).first
    user ||= User.create!(provider: auth.provider, uid: auth.uid, last_name: name_split[0], first_name: name_split[1], email: auth.info.email, username: auth.info.email, password: Devise.friendly_token[0, 20])
      user
  end

Devise.rb:

config.omniauth :facebook, "APP_ID", "APP_SECRET", callback_path: "/api/v1/users/auth/facebook/callback"

The Routes File:

devise_for :users, controllers: { registrations: "api/v1/users/registrations", sessions: "api/v1/users/sessions", passwords:  "api/v1/users/passwords", omniauth_callbacks: 'api/v1/users/omniauth_callbacks' }

The Omniauth Controller:

# frozen_string_literal: true
class Api::V1::Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
  def passthru
    Rails.logger.info "PassThru"
    Rails.logger.debug request.env['omniauth.auth']
    super
  end

  def facebook
    Rails.logger.info "Check out this info!"
    @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"].except(:extra) # Removing extra as it can overflow some session stores
      redirect_to new_user_registration_url
    end
  end

  def auth_data
    Rails.logger.debug "Auth_DATA"
    request.env["omniauth.auth"]
  end

  def failure
    Rails.logger.info "FAIL"
    Rails.logger.info root_path
    redirect_to root_path
  end

  def callback
    Rails.logger.info "HELLO"
  end
end

The Omniauth middleware in initializers dir

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :facebook, "APP_ID", "APP_SECRET",
    scope: 'email,user_birthday,read_stream', display: 'popup'
end

and in finally the link in the view file:

(link_to "Register with Facebook", user_facebook_omniauth_authorize_path, method: :post)

and this is the log when I click on the link in the view:

| I, [2022-01-24 13:30:12 #1]  INFO -- : Started GET "/api/v1/users/auth/facebook" for 172.20.0.1 at 2022-01-24 13:30:12 +0000
| I, [2022-01-24 13:30:12 #1]  INFO -- : PassThru
| D, [2022-01-24 13:30:12 #1] DEBUG -- : nil

I have made sure the FB credentials are right (APPID n SECRET) and FB app is not yet live so it can accept my localhost url as redirect url.

but still I can not understand what's the wrong I am doing here.. Kindly give your valuable feedback here... as I am also new to ruby...

I am running ruby@3.1.0 and rails@6.0.4.4

Thanks in advance.

Upvotes: 1

Views: 238

Answers (0)

Related Questions