I need to modify how Devise decides if a user account is confirmed.
I have set up 2 step registration where a user enters an email then sets their password after clicking a link in their email.
This works fine but I have a scenario where a logged in user can create a user and send them a link to confirm their account. This is sent manually via a custom mailer (this includes the email address of the user who requested their account as well as the confirmation_token) which means a user is getting 2 emails if their account is created via another user, the standard devise confirmation_instructions and the custom mail 'invite'.
Following this I set the skip_confirmation! before the save which means only the email I send gets to the user but since this sets the confimed_at field a user cannot set their password as it generates an error saying account is already confirmed.
How can I change the confirmed? method to also check that the password is not null?
Here is my overridden confirmations controller for my 2 step registration process:
class ConfirmationsController < Devise::ConfirmationsController
def show
self.resource = resource_class.find_by_confirmation_token(params[:confirmation_token])
super if resource.confirmed?
end
def confirm
self.resource = resource_class.find_by_confirmation_token(params[resource_name][:confirmation_token])
if resource.update_attributes(params[resource_name].except(:confirmation_token)) && resource.password_match?
self.resource = resource_class.confirm_by_token(params[resource_name][:confirmation_token])
set_flash_message :notice, :confirmed
sign_in_and_redirect(resource_name, resource)
else
render :action => "show"
end
end
end
Here is the custom mailer:
class MyMailer < Devise::Mailer
helper :application # gives access to all helpers defined within `application_helper`.
def invite(sender, recipient)
@sender = sender
@recipient = recipient
mail( :to => recipient.email,
:subject => "Account created by #{sender.email}",
:from => "noreply@noreply.com"
)
end
end
code in a controller where a logged in user creates a new user (showing section applicable)
.
.
.
newContact = User.create(:email => params[:contact_email])
newContact.skip_confirmation!
if newContact.save
MyMailer.invite(current_user, newContact).deliver
.
.
.
end
.
.
.