Search code examples
ruby-on-railsfacebookdeviseomniauth-facebook

Is the provider and uid stored somewhere even after deleting a user using Devise and Omniauth?


Is the provider and uid stored in another table after deleting a user using Devise and Omniauth?

In my Rails 4 app I was trying to integrate Omniauth-facebook with Devise, following the Devise and Omniauth Overview Guide

I successfully logged in (and create an account) with my facebook account. Then as another admin user, I deleted the account used to signup with Facebook.

I tried to log in with Facebook (to recreate my account) with the same facebook account, and this time I was redirected to the regular email sign up page.

I went in my Rails Console and checked if there are any users using my facebook provider and UID and found an object ActiveRecord::Relation

2.1.3 :001 > user = User.where(provider: "facebook", uid: 'MYFACEBOOKUID')
  User Load (0.5ms)  SELECT "users".* FROM "users"  WHERE "users"."provider" = 'facebook' AND "users"."uid" = 'MYFACEBOOKUID'
 => #<ActiveRecord::Relation []> 

I believe this ActiveRecord::Relation, was not destroyed when I deleted my account using Facebook. And now it prevents me from signing up with my Facebook account.

How can I delete this relation?

My User Model:

class User < ActiveRecord::Base
  rolify before_add: :before_add_method
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable,
         :omniauthable, :omniauth_providers => [:facebook]

   before_create :set_default_role

   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
       user.last_name = auth.info.last_name
       user.username = auth.info.nickname
       user.facebook_page = auth.extra.raw_info.link
       user.gender = auth.extra.raw_info.gender
     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?
          user.first_name = data["first_name"] if user.first_name.blank?
          user.last_name = data["last_name"] if user.last_name.blank?
          user.valid?
          # user.email = data["email"] if user.email.blank?
        end
      end
    end

   private

   def before_add_method(role)
    # do something before it gets added
   end

   def set_default_role
     if User.count == 0
       self.add_role :admin
     else
       self.add_role :user unless User.count == 0
     end
   end

end

Solution

  • There is no relations to delete, the result is empty. You need to debug to find out why was the new user not saved.

    In your controller use the following for debugging:

    puts request.env["omniauth.auth"]
    puts @user.errors.to_a