Devise: Is it possible to NOT send a confirmation email in specific cases ? (even when confirmable is active)
Asked Answered
C

5

16

Here is my situation, I use devise to allow users to create account on my site and manage their authentication. During the registration process I allow customers to change some options, leading to an actually different account being created but still based on the same core user resource. I would like to choose not to send a confirmation email for some of those account types. I don't care if the account do not get confirmed and user cannot log in, that's ok, no pb with that. How would I go about doing that ? Thanks, Alex

Countless answered 25/1, 2011 at 6:42 Comment(0)
C
28

Actually it's quite easy once I dig a little deeper. Just override one method in your User model (or whatever you are using):

    # Callback to overwrite if confirmation is required or not.
    def confirmation_required?
      !confirmed?
    end

Put your conditions and job's done !

Alex

Countless answered 25/1, 2011 at 8:33 Comment(1)
The mail won't be sent in an resend_confirmation or a send_reconfirmation, which are usefull ...Inapt
F
18

If you just want to skip sending the email but not doing confirmation, use:

# Skips sending the confirmation/reconfirmation notification email after_create/after_update. Unlike
# #skip_confirmation!, record still requires confirmation.
@user.skip_confirmation_notification!

If you don't want to call this in your model with a callback overwrite this method:

def send_confirmation_notification?
  false
end
Frond answered 6/10, 2015 at 20:4 Comment(0)
E
11

You can also simply add the following line of code in your controller before creating the new user:

@user.skip_confirmation!
Eulalie answered 14/12, 2012 at 1:10 Comment(1)
If you're overloading create action and using super do |user| for customization, it's too late to skip, devise already sent the email. This should go in build_resource, for instance.Fallacious
A
9

I don't know if Devise added this after the other answers were submitted, but the code for this is right there in confirmable.rb:

  # If you don't want confirmation to be sent on create, neither a code
  # to be generated, call skip_confirmation!
  def skip_confirmation!
    self.confirmed_at = Time.now
  end
Automat answered 23/8, 2011 at 0:32 Comment(2)
Note that you should call this before saving the user, otherwise the Confirmation instructions email will be sent.Byler
There's also a similar skip_reconfirmation! methodBushido
R
6

I was able to do something similar with the functions:

registrations_controller.rb

def build_resource(*args)
    super
    if session[:omniauth] # TODO -- what about the case where they have a session, but are not logged in?
      @user.apply_omniauth(session[:omniauth])
      @user.mark_as_confirmed # we don't need to confirm the account if they are using external authentication
      # @user.valid?
    end
  end

And then in my user model:

user.rb

  def mark_as_confirmed
    self.confirmation_token = nil
    self.confirmed_at = Time.now
  end
Ray answered 28/4, 2011 at 0:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.