dimanche 19 avril 2015

Rails 4, devise, omniauth, facebook: problems when regular sign up involves nested forms

I am trying to set up facebook login integration with omniauth and devise.


Note that I have a MEMBER model which contains the devise info and a USER model that contains the member profile info (to keep them separate)


On the sign up page, the user has a nested form in the member sign up form and receives all the user data at that time. Once the member is saved, the email value for the saved member is also entered into the user table like so



class Member < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :confirmable, :omniauthable

after_save :update_user

def update_user
user.email = self.email
user.save
end


I have the standard model method for the facebook data processing.....



def self.process_omniauth(auth)
where(provider: auth.provider, uid: auth.uid, email: auth.info.email).first_or_create do |member|
member.provider = auth.provider
member.uid = auth.uid
member.email = auth.info.email
end
end


But of course, in this case, there is no nested form so the 'user' is not instantiated and it throws an error when the update_user method is called on saving the member.


How can I modify this method above to instantiate a new user, when signing up via the facebook pathway?


Aucun commentaire:

Enregistrer un commentaire