Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Devise - create user account with confirmed without sending out an email?

I integrated devise with facebook. Now when I create a user account after the user has logged in with his/her facebook account,

  user = User.create(:email => data["email"],                       :password => Devise.friendly_token[0,20])    user.confirmed_at = DateTime.now   user.save! 

even though the account has been confirmed, an confirmation email is still fired. Any idea how I can turn the email firing off?

like image 997
disappearedng Avatar asked Sep 19 '11 00:09

disappearedng


2 Answers

The confirm callback happens after create, so it's happening on line 1 of your example, before you set confirmed_at manually.

As per the comments, the most correct thing to do would be to use the method provided for this purpose, #skip_confirmation!. Setting confirmed_at manually will work, but it circumvents the provided API, which is something which should be avoided when possible.

So, something like:

user = User.new(user_attrs) user.skip_confirmation! user.save! 

Original answer:

If you pass the confirmed_at along with your create arguments, the mail should not be sent, as the test of whether or not an account is already "confirmed" is to look at whether or not that date is set.

User.create(   :email => data['email'],    :password => Devise.friendly_token[0,20],    :confirmed_at => DateTime.now ) 

That, or just use new instead of create to build your user record.

like image 134
numbers1311407 Avatar answered Oct 15 '22 22:10

numbers1311407


If you just want to prevent sending the email, you can use #skip_confirmation_notification, like so:

user = User.new(your, args) user.skip_confirmation_notification! user.save! 

See documentation

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

like image 44
Benjamin Crouzier Avatar answered Oct 15 '22 22:10

Benjamin Crouzier