title: Omniauth Facebook login redirects to signup if user exists
body: <p>I'm playing around with the <code>omniauth-facebook</code> gem to log into a devise session through a facebook account.  When I click the "Sign in with facebook" link, everything goes well: a new account is created, I'm signed in and bounce back to the homepage with a message confirming my new session (very good!).</p>
  
  <p><strong>Problem:</strong> However when an account already exists, upon clicking the link I am redirected to the <code>user/sign_up</code> page.  I've been following <a href="https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview" rel="nofollow noreferrer">this documentation</a> from the Devise wiki.  There is a good deal of documentation on similar errors <a href="https://stackoverflow.com/questions/23839456/rails-omniauth-facebook-login-redirects-to-sign-up">here</a>, <a href="https://stackoverflow.com/questions/24963840/devise-omniauth-facebook-redirecting-to-signup">here</a>, <a href="https://stackoverflow.com/questions/31373640/omniauth-facebook-redirects-to-registration-screen-instead-of-creating-user">here</a> and <a href="https://stackoverflow.com/questions/18597470/redirect-to-page-after-facebook-sign-up">here</a>.  Each of the solutions, however, are already implemented in my app (as far as I can tell) OR (in the case of the last link) seem to be based on an older configuration model that seems sufficiently different from the wiki that I'm not sure it's applicable.</p>
  
  <p><strong>My best guess</strong> is that it has something to do with the callbacks controller, as <code>@user.persisted?</code> seems to be coming up false.This leads me to believe that my definition of <code>@user</code> is not correct.  See below:</p>
  
  <pre><code>class Users::OmniauthCallbacksController &lt; Devise::OmniauthCallbacksController
    def facebook
      logger.debug "Inside 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"])
      logger.debug "User is #{@user}"
  
      if @user.persisted?
        logger.debug "@user.persisted?"
        debugger
        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?
        logger.debug "user exists"
      else
        session["devise.facebook_data"] = request.env["omniauth.auth"]
        redirect_to new_user_registration_url
      end
    end
  
    def failure
      redirect_to root_path, alert: "Login failed"
    end
  end
  </code></pre>
  
  <p>Additionally, my user model is as follows:</p>
  
  <pre><code>class User &lt; ActiveRecord::Base
      # Include default devise modules. Others available are:
      # :confirmable, :lockable, :timeoutable and :omniauthable
      devise :database_authenticatable, :registerable,
                   :recoverable, :rememberable, :trackable, :validatable, :omniauthable, :omniauth_providers =&gt; [:facebook]
  
      def self.from_omniauth(auth)
          where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
              user.provider = Devise.friendly_token[0,20]
              user.email = auth.info.email
              user.password = Devise.friendly_token[0,20]
              user.fname = auth.info.first_name
              user.lname = auth.info.last_name
          end
      end
  end
  </code></pre>
  
  <p>Any suggestions would be certainly welcome!  Thanks in advance.</p>