Scott, your idea is correct. I struggled with this problem to no avail. I tried to override 'has_secure_password' and it just won't work. No matter where I pasted the code.
Instead, I have the following:
class User < ActiveRecord::Base has_secure_password validates_presence_of :password, :on => :create, :if => :password_required # Associations has_many :authentications # Callbacks before_validation :no_password_omniauth # Gets set to true if the caller is trying to authenticate with omniauth. @called_omniauth = false # Build new omniauth users def apply_omniauth(omniauth) authentications.build( :provider => omniauth['provider'], :uid => omniauth['uid']) self.first_name = omniauth['user_info']['first_name'] if self.first_name.blank? self.last_name = omniauth['user_info']['last_name'] if self.last_name.blank? self.email = omniauth['user_info']['email'] if omniauth['user_info']['email'] && self.email.blank? @called_omniauth = true end def password_required return false if @called_omniauth == true (authentications.empty? || !password.blank?) end private def no_password_omniauth self.password_digest = 0 unless password_required end end
The apply_omniauth method is called from the controller when someone tries to authenticate or register.
Thanks for the idea you nailed.
crankin
source share