Login with Facebook when user is registered, Rails 5, Omniouth with Device

64 Views Asked by At

I have this User model:

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.fname = auth.info.name   
        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

and this omniouth_callbacks_controller:

def facebook
    @user = User.from_omniauth(request.env["omniauth.auth"])
    @user.confirmed_at = Time.now
    if @user.persisted?
        sign_in_and_redirect @user, :event => :authentication
        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

Everything works, but if user is registered without Facebook and then try to login with Facebook, it is not logged, because there are user row in the table in the database.

I want if user is registered in the site without socials and login with Facebook, to do that and site profile to be connected with Facebook profile. Is there Omniouth method builtin for this case ? If not, how to do it ?

0

There are 0 best solutions below