sorcery icon indicating copy to clipboard operation
sorcery copied to clipboard

external? turned true when password and password confirmation is nil

Open takashi opened this issue 9 years ago • 2 comments

I try to implement simple password authentication and oauth login with sorcery to my app.

when I try to signup without password and password confirmation, external? has been true

here's my code

class User < ActiveRecord::Base
  authenticates_with_sorcery!
  authenticates_with_sorcery! do |config|
    config.authentications_class = Authentication
  end

  validates :password, presence: true, if: :password_required?
  validates :password, confirmation: true, if: :password_required?
  validates :password, length: { minimum: 8 }, allow_blank: true


  private
  def password_required?
    new_record? && !external?
  end
end

In password_required?, external? has been true and validation has been ignored when password and password_confirmation is nil.

How can I turn on validation in this case?

takashi avatar Feb 28 '16 11:02 takashi

Hey @takashi, the implementation of external? in Sorcery is not great. You can change it for example to:

def external?
  authentications.present?
end

This way users will not require password only if they have at least one authentication assigned (i.e. that they come from some oAuth provider)

arnvald avatar Mar 01 '16 16:03 arnvald

Hi @arnvald, thank you for giving me the good solution. I'd like to implement ur solution as default implementation of sorcery's external? . is there any reason not to implement external? this way?

takashi avatar Mar 10 '16 13:03 takashi