0

I keep getting the following page when trying to log in using Facebook in Rails 5.2: Sorry, something went wrong. We're working on getting this fixed as soon as we can.

The details to the app were as follows:

enter image description here enter image description here

The gems I have installed are: gem 'omniauth', gem 'omniauth-facebook', gem 'omniauth-rails_csrf_protection', '~> 1.0'

In my devise.rb, I added the following line in the end: config.omniauth :facebook, '1219002185454860', 'my app password here', scope: 'email', info_fields: 'email,name'

This is my user.rb:

# frozen_string_literal: true

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable

  devise :omniauthable, omniauth_providers: %i[facebook]

  validates :name, presence: true
  validates :username, presence: true, uniqueness: { case_sensitive: false, index: { unique: true } },
                       length: { minimum: 4, maximum: 20 }
  validates :birthdate, presence: true
  validates :profile_pic_url, format: { with: %r{\Ahttps?://.*\z}, message: 'must be a valid URL' }, allow_blank: true
  validates :is_private, inclusion: { in: [true, false] }, default: false
  validate :username_format

  def username_format
    return if username.blank?
    return if /^[a-zA-Z0-9_]+$/.match?(username)

    errors.add(:username, 'must only contain letters, numbers, and underscores')
  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

  def self.from_omniauth(auth)
    find_or_create_by(provider: auth.provider, uid: auth.uid) do |user|
      user.email = auth.info.email
      user.password = Devise.friendly_token[0, 20]
      user.name = auth.info.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
end

My routes.rb

# frozen_string_literal: true

Rails.application.routes.draw do
  root 'pages#index'
  devise_for :users, controllers: { omniauth_callbacks: 'users/omniauth_callbacks' }

end

My _link.html.erb section which is rendered onto the devise page <%= link_to "Sign in with Facebook", user_facebook_omniauth_authorize_path, method: :post, data: { turbo: false } %>

And here's the user/omniauth_callbacks_controller.rb

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
  # See https://github.com/omniauth/omniauth/wiki/FAQ#rails-session-is-clobbered-after-callback-on-developer-strategy
  skip_before_action :verify_authenticity_token, only: :facebook

  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"].except(:extra) # Removing extra as it can overflow some session stores
      redirect_to new_user_registration_url
    end
  end

  def failure
    redirect_to root_path
  end
end

And this is my schema.rb:

# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2023_08_20_100434) do

  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"

  create_table "users", force: :cascade do |t|
    t.string "email", default: "", null: false
    t.string "encrypted_password", default: "", null: false
    t.string "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "name"
    t.string "username"
    t.date "birthdate"
    t.string "profile_pic_url"
    t.boolean "is_private", default: false
    t.string "provider"
    t.string "uid"
    t.index ["email"], name: "index_users_on_email", unique: true
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
    t.index ["username"], name: "index_users_on_username", unique: true
  end

end

I am so lost as to what I'm doing wrong, any help would be greatly appreciated!

I've gone through https://github.com/heartcombo/devise/wiki/OmniAuth%3A-Overview, https://github.com/heartcombo/devise/issues/5236, etc - however, I'm not sure what the problem exactly is. I've cleared my cookies, as well.

0 Answers0