RailsCasts Pro episodes are now free!

Learn more or hide this

Alexis Ramis's Profile

GitHub User: zerocool4u2

Comments by Alexis Ramis

Avatar

Finally i add a counter for clean the session, after a couple of refreshes or a couple of errors it will show the normal page

user.rb
  def self.from_omniauth(auth)
    where(auth.slice(:provider, :uid)).first_or_create do |user|
      user.provider = auth.provider
      user.uid = auth.uid
      user.email = auth.info.email
    end
  end

  #persist user in session on validation error with omniauth
  def self.new_with_session(params, session)
    if session["devise.user_attributes"]
      new(session["devise.user_attributes"], without_protection: true) do |user|
        user.attributes = params
        user.valid?
        session["count_errors"] = session["count_errors"] + 1
        session["devise.user_attributes"] = nil if session["count_errors"] == 2
      end
    else
      super
    end
  end

  #validation for password on omniauth
  def password_required?
    super && self.provider.blank?
  end

  #password for update if blank on omniauth
  def update_with_password(params, *options)
    if encrypted_password.blank?
      update_attributes(params, *options)
    else
      super
    end
  end

  def has_no_password?
    self.encrypted_password.blank?
  end
omniauth_callbacks_controller.rb
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
  def all
      # 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?
        flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => user.provider.titleize.split(" ").first
        sign_in_and_redirect user, :event => :authentication
      else
        session["count_errors"] = 0 if session["devise.user_attributes"] == nil
        session["devise.user_attributes"] = user.attributes
        redirect_to new_user_registration_url
      end
  end
  alias_method :twitter, :all
  alias_method :google_oauth2, :all
end
Avatar

Hi, i'm having troubles with password_required and new_with_session

user.rb
  def self.from_omniauth(auth)
    where(auth.slice(:provider, :uid)).first_or_create do |user|
      user.provider = auth.provider
      user.uid = auth.uid
      user.email = auth.info.email
    end
  end

  #persist user in session on validation error with omniauth
  def self.new_with_session(params, session)
    if session["devise.user_attributes"]
      new(session["devise.user_attributes"], without_protection: true) do |user|
        user.attributes = params
        user.valid?
      end
    else
      super
    end
  end

  #validation for password on omniauth
  def password_required?
    super && self.provider.blank?
  end

  #password for update if blank on omniauth
  def update_with_password(params, *options)
    if encrypted_password.blank?
      update_attributes(params, *options)
    else
      super
    end
  end

  def has_no_password?
    self.encrypted_password.blank?
  end

The session are always there, imagine this case, you sign up with twitter, the system asks you for a email, you regrets and want to use normal register, you can't because there isn't the password field and the provider info is still in cache, i need to know where to clear that session :/

Similar problem when you want to update the blank password with an error, it would f.object.encrypted_password.present? would return true in that case, so i add that has_no_password? method for that.

Avatar

i have that problem and was going to other way for get the solution, thanks!