title: How to allow users login with both registered account and facebook account?
body: <p>I used devise to allow users register and login within the site. I also used omniauth-facebook to allow users login with their facebook accounts. However, people who registered first with the same email as their facebook-email cannot login with their facebook account.I want to use both facebook login and site registration.</p>
  
  <p>This is my user.rb</p>
  
  <pre><code>class User &lt; ActiveRecord::Base
  
    devise :database_authenticatable, :registerable,
       :recoverable, :rememberable, :trackable, :validatable, :omniauthable
  
    def self.find_for_facebook_oauth(auth, signed_in_resource=nil)
      user = User.where(:provider =&gt; auth.provider, :uid =&gt; auth.uid).first
      unless user
        user = User.create(name:auth.extra.raw_info.name,
                           provider:auth.provider,
                           uid:auth.uid,
                           email:auth.info.email,
                           password:Devise.friendly_token[0,20]
                           )
      end
      user
    end
  end
  </code></pre>
  
  <p>And omniauth_callbacks_controller.rb</p>
  
  <pre><code>class Users::OmniauthCallbacksController &lt; Devise::OmniauthCallbacksController
    def facebook
      @user = User.find_for_facebook_oauth(request.env["omniauth.auth"], current_user)
  
      if @user.persisted?
        sign_in_and_redirect @user, :event =&gt; :authentication #this will throw if @user is not activated
        set_flash_message(:notice, :success, :kind =&gt; "Facebook") if is_navigational_format?
      else
        session["devise.facebook_data"] = request.env["omniauth.auth"]
        redirect_to new_user_registration_url
      end
    end
  end
  </code></pre>