ruby on rails - Storing unconfirmed email column on the sign up -
ruby on rails - Storing unconfirmed email column on the sign up -
i'm using devise confirmable module enabled.
i realised unconfirmed email column used when user has confirmed email , wants alter it.
i have next issue:
the user "a" signed email belongs user "b". user "a" never success on confirm account. after that, user "b" seek sign own email (already used user "a" before). user "b" can't sign because email beingness used else.i think on sign page, email should stored in unconfirmed email column avoid behavior above.
there workaround prevent this?
it interesting question. think simplest way (but not sure best way) prevent problem alter email uniqueness validation , create work if email confirmed.
to should disallow validatable
module in user model , implement validations manually.
you can re-create default validations here https://github.com/plataformatec/devise/blob/master/lib/devise/models/validatable.rb, validates_uniqueness_of
. implement own email uniqueness validation:
class user < activerecord::base # not include :validatable module here devise :confirmable, :database_authenticatable, :registerable, ... # own validations ... validates_uniqueness_of :email, allow_blank: true, if: lambda { |u| u.email_changed? && u.confirmed? } ...
edited:
my solution not right. first of all, email can't confirmed if changed. if solution work , allow user register existing unconfirmed email, when user seek confirm email fail anyway.
the right solution is:
1) add together validation prevent registration if email exists , confirmed.
2) redefine #confirme!
method don't confirm email if exists , confirmed
3) (not neŃessary) redefine #after_confirmation
method remove other unconfirmed accounts email
class user < activerecord::base # not include :validatable module here devise :confirmable, :database_authenticatable, :registerable # , ... validate :confirmed_email_uniqueness scope :with_confirmed_email, -> { where.not(confirmed_at: nil) } scope :with_unconfirmed_email, -> { where(confirmed_at: nil) } def confirm! homecoming false if confirmed_email_exists? # or add together validation error, or raise exception super end private def confirmed_email_uniqueness errors.add(:email, "already exists") if email_changed? && confirmed_email_exists? end def confirmed_email_exists? user.with_confirmed_email.where(email: self.email).exists? end protected def after_confirmation user.with_unconfirmed_email.where(email: self.email).destroy_all super end ...
ruby-on-rails ruby-on-rails-4 devise devise-confirmable
Comments
Post a Comment