Skip to content

Instantly share code, notes, and snippets.

@nicoolas25
Created November 13, 2012 13:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nicoolas25/4065810 to your computer and use it in GitHub Desktop.
Save nicoolas25/4065810 to your computer and use it in GitHub Desktop.
Allow only one connection before confirmation with Devise.
module OneConnectionBeforeConfirm
extend ActiveSupport::Concern
included do
before_create :generate_confirmation_token
after_create :send_on_create_confirmation_instructions
end
# This actions are not properly trigerred because of the rest of our modifications.
before_create :generate_confirmation_token
after_create :send_on_create_confirmation_instructions
# Alias the original devise's confirmed? method
alias :strictly_confirmed? :confirmed?
# Override the confirmed? method, this will allow an unconfirmed user to sign in
# just after sign up.
def confirmed?
sign_in_count < 2 || strictly_confirmed?
end
protected
# Must use the strictly_confirmed? method here to avoid confirmation issues.
def pending_any_confirmation
if (!strictly_confirmed? || pending_reconfirmation?)
yield
else
self.errors.add(:email, :already_confirmed)
false
end
end
end
class User
include Mongoid::Document
devise [...], :confirmable
include OneConnectionBeforeConfirm
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment