1

I want to create profile automatically when user is created by devise signup Or by Omniauth-facebook. I have User Model (user.rb)

 class User < ActiveRecord::Base
 devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable,
     :omniauthable, :omniauth_providers => [:facebook]

has_many :listings, dependent: :destroy
has_many :bookings, dependent: :destroy
has_one :profile, dependent: :destroy

before_create :build_profile

  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]
#Profile.where(:user_id => user.id).update_all(name: auth.info.name)
user.build_profile(name: auth.info.name)
   end
  end
 end

But if I use before_create :build_profile and sign_in with Facebook then two profiles are created and if I use user.build_profile(name: auth.info.name) in self.from_omniauth(auth) then if I sign_up with Devise ..no profile is created. so where in this code I Build profile ? 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.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 => "Faceboo") 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

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.