Search code examples
ruby-on-railsrubydeviseomniauth

Rails Devise Omniauth new_user_registration_url


I had implemented devise gem and omniauth-facebook before few months into my app and everything was worked as usual, until now when I deleted my fb user from db. Now I am trying to sign in again as nonexisting user and all the time I am redirected to Devise Sign Up form.

So I cant log in/ register my FB user into my app.

What can cause this type of problem?

routes.rb

  devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }

omniouth_callback_controller.rb

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
  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"]
      redirect_to new_user_registration_url
    end
  end

  def failure
    redirect_to root_path
  end
end

user.rb model

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

  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)
    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.name = auth.info.name   # assuming the user model has a name
      user.image = auth.info.image # assuming the user model has an image
    end
  end

rails routes

     new_user_session GET      /users/sign_in(.:format)                 devise/sessions#new
                    user_session POST     /users/sign_in(.:format)                 devise/sessions#create
            destroy_user_session DELETE   /users/sign_out(.:format)                devise/sessions#destroy
user_facebook_omniauth_authorize GET|POST /users/auth/facebook(.:format)           users/omniauth_callbacks#passthru
 user_facebook_omniauth_callback GET|POST /users/auth/facebook/callback(.:format)  users/omniauth_callbacks#facebook
               new_user_password GET      /users/password/new(.:format)            devise/passwords#new
              edit_user_password GET      /users/password/edit(.:format)           devise/passwords#edit
                   user_password PATCH    /users/password(.:format)                devise/passwords#update
                                 PUT      /users/password(.:format)                devise/passwords#update
                                 POST     /users/password(.:format)                devise/passwords#create
        cancel_user_registration GET      /users/cancel(.:format)                  devise/registrations#cancel
           new_user_registration GET      /users/sign_up(.:format)                 devise/registrations#new
          edit_user_registration GET      /users/edit(.:format)                    devise/registrations#edit
               user_registration PATCH    /users(.:format)                         devise/registrations#update
                                 PUT      /users(.:format)                         devise/registrations#update
                                 DELETE   /users(.:format)                         devise/registrations#destroy
                                 POST     /users(.:format)                         devise/registrations#create

Solution

  • I found the reason in user.rb model I had one association belongs_to:somemodel and that I was put there for testing some model relations.

    So commenting this line I am logged in by facebook!