if @user.persisted? with Facebook omniauth | Devise

4.2k Views Asked by At

My Omniauth_callbacks_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.find_for_facebook_oauth(request.env["omniauth.auth"], current_user)

        if @user.persisted?
            # This will throw if @user is not activated
            sign_in_and_redirect @user, event: :authentication
            if is_navigational_format?
                set_flash_message(:notice, :success, kind: "Facebook")
            end
        else
            session["devise.facebook_data"] = request.env["omniauth.auth"]
            redirect_to new_user_registration_url
        end
    end
end

No problems, no Logs. BUT when someone tries to Connect via Facebook it throws them simply to the default Registration Form.

http://localhost:3000/sign_up#_=_

My User Model

class User < ActiveRecord::Base

  devise :database_authenticatable, :registerable, #:recoverable
         :rememberable, :trackable, :validatable, :omniauthable,
                 :omniauth_providers => [:facebook]


    # Facebook Settings
    def self.find_for_facebook_oauth(auth, signed_in_resource = nil)
        user = User.where(provider: auth.provider, uid: auth.uid).first
        if user.present?
            user
        else
            user = User.create(first_name:auth.extra.raw_info.first_name,
                                                 last_name:auth.extra.raw_info.last_name,
                                                 facebook_link:auth.extra.raw_info.link,
                                                 provider:auth.provider,
                                                 uid:auth.uid,
                                                 email:auth.info.email,
                                                 password:Devise.friendly_token[0,20])
        end
    end



    validates :first_name, :presence => true
    validates :email, :presence => true
    validates :user_name, :presence => true, :uniqueness => true

    has_many :clips
    has_one :show, dependent: :destroy

    # Profile Page Avatar
    has_attached_file :avatar, styles: { avatar: "64x64#"},
                                        :default_url => "http://placehold.it/150x150&text=Missing"

    validates_attachment :avatar,
                                             content_type: { content_type: ['image/jpeg', 'image/jpg', 'image/png', 'image/gif'] },
                                             size: { less_than: 5.megabytes }

    # Profile Page Cover
    has_attached_file :profile_cover, styles: { cover: "870x150#"},
                                        :default_url => "http://placehold.it/150x150&text=Missing"

    validates_attachment :profile_cover,
                                             content_type: { content_type: ['image/jpeg', 'image/jpg', 'image/png', 'image/gif'] },
                                             size: { less_than: 5.megabytes }


    # For Using the username instead of ID in the Link
    def to_param
        user_name
    end

end
1

There are 1 best solutions below

3
On BEST ANSWER

I'm not 100% sure the reason as there are some other info involved.

I would suggest:

  1. Change this line

    @user ||= User.find_for_facebook_oauth ...
    

    To

    @user = User.find_for_facebook_oauth
    

    This is to eliminate other possible effect, and there is little point to cache @user as this action will not be hit frequently.

  2. Make sure you have provider, uid, name columns added and migrated.

  3. The reason should be @user is not persisted so there may be something wrong when creating the user. You can change create to create! to see what error thrown.

    If still not sure, debug into this User method.

Update

Okay, got the reason. You have a validation:

validates :user_name, :presence => true, :uniqueness => true

But in Facebook attributes, there is no such attribute :user_name, so saving will be fail.

Fix is just to use a method to add this attribute to Facebook hash, or not validate this attr.