I want to configure devise to send a confirmation email when the user has changed their password as a security measure. I'd prefer to re-use my devise mailers if possible. Any ideas how to do this?
Untested, but I'd try to do this within your User model with a simple after_update callback:
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable, :recoverable # whatever
after_update :send_password_changed_notification
# attr_accessible, validations, ...
private
def send_password_changed_notification
# Send email with the mailer of your choice,
# e. g. your existing custom Devise mailer:
YourDeviseMailer.password_changed_notification(self).deliver if password_changed?
end
end
I would configure the update user action, you can read on how to do that in their documentation. Check how devise handles confirmations of new registered users in registration action and re-use that code in your new reset password action.
Related
I have 3 different devise models. Two using email & password for login and works great. But For the third one I need to use mobile & OTP based login instead of email & password. So the devise session controller only receives mobile & the otp as params.
I have changed from email to mobile by using authentication_keys
class Customer < ApplicationRecord
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable, authentication_keys: [:mobile]
def email_required?
false
end
end
I am using devise-jwt for jwt response & overriding sessions_controller like this
class Api::Customers::V1::Auth::SessionsController < Devise::SessionsController
def create
super { #token = current_token }
end
private
def current_token
request.env['warden-jwt_auth.token']
end
end
But now am getting password blank validation error. How can I disable password validation & change to do an otp verification instead?
PS: I have seen several resources on two factor auth . But they all work with password validation.
I currently have an app that allows users to invite other users to their organization. How can I make it so that devise will send a reset password link to the users that have been invited?
Add recoverable and use the send_reset_password_instructions method.
class User < ActiveRecord::Base
devise :recoverable
...
end
User.find(1).send_reset_password_instructions
My users table has unique indexes on the email and username fields. Every now and then the uniqueness constraint will be broken, and a ActiveRecord::RecordNotUnique exception will be thrown. It can happen due to a race condition in the rails uniqueness validation, when the user submits the registration form twice in rapid succession, or when two users attempt to register with the same username, at the same time.
When the exception is caused by two successive registration requests I would like to sign the user in. Otherwise, when the uniqueness constraint is broken by the rare case of two users registering with the same username, I'd like to display the usual "taken" error.
To do so I've overridden the create action:
class RegistrationsController < Devise::RegistrationsController
def create
begin
super
rescue ActiveRecord::RecordNotUnique
user = User.find_by_email(params[:user][:email])
if user.present? && user.valid_password?(params[:user][:password])
# The credentials are valid for the existing user. We can
# sign them in.
sign_in(:user, user)
respond_with user, :location => after_sign_in_path_for(user)
else
# The credentials are invalid.
# This should only happen if multiple users register with the
# same email at the same time. Now we can simply attempt to
# register the user again, knowing it will fail, in order to
# generate the appropriate error messages.
super
end
end
end
end
Is there a way to make Devise handle ActiveRecord::RecordNotUnique exceptions and achieve something similar to what I have done?
i had the same problem.
The exception is thrown because of the add_index :email, unique => true, added by devise to your user model (have a look in your migrations). This adds a uniqueness constraint at database level, overriding frameowrk validations.
Actually i decided to throw off the uniqueness of the email index, so that i can manage everything with rails validations, but i actually don't know if it's completely correct, nor if it supposed to work this way as it's the first time i'm using devise with STI.
hope it helps.
Just add :validatable in your resource model. Most likely in user.rb. It should look like this:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable,
:registerable,
:recoverable,
:rememberable,
:trackable,
:validatable
end
Make sure :validatable is included. Or else it won't capture the errors from database.
Recently I added the confirmable module to my User class. I already have a quite nice mailing system (Sidekiq, Sendgrid...) in my app, so I created my own "confirm account" mail. The problem now is to disable Devise from sending its default email. Is there any way to completely disable the Devise mailing system?
Added:
I want to maintain the confirmable module, as I am using its attributes and routes.
I can't use skip_confirmation! because I want the users to confirm their account.
I just want to disable Devise mails.
Use the source, Luke:
# lib/devise/models/confirmable.rb
# A callback method used to deliver confirmation
# instructions on creation. This can be overriden
# in models to map to a nice sign up e-mail.
def send_on_create_confirmation_instructions
send_devise_notification(:confirmation_instructions)
end
So override this method in your model to do nothing.
Try overriding the following devise method in your model:
def confirmation_required?
!confirmed?
end
or use skip_confirmation!:
user = User.new(params)
user.skip_confirmation!
user.save!
Use skip_confirmation! method before saving any object.
def create
#user = User.new(params[:user])
#user.skip_confirmation!
#user.save!
end
I think just removing
:confirmable
from the user model should do it
or have you tried disabling
config/environments/development.rb
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
I recommend you
User.skip_reconfirmation!
That is skip confirm mail and update email not to use "confirm!"
remove (:confirmable) from devise model
ex:- here my devise model is User
here I used like this.
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,:omniauthable
end
Here is a Ruby class:
class User
devise :trackable, :confirmable
end
For most instances I want :confirmable to be present, but for some instances, I would like to remove :confirmable before instantiation.
QUESTION: How to remove :confirmable on-the-fly?
I would rather avoid creating a separate class.
devise :confirmable adds a number of methods to your model, one of which is skip_confirmation!:
If you don’t want confirmation to be sent on create, neither a code to
be generated, call skip_confirmation!
Example:
user = User.new
user.skip_confirmation!
You will need the migrations for both :trackable and :confirmable in any case for your DB.
Wouldn't it be easier to just have :confirmable defined for both cases, but in the case you don't want it, you can automatically confirm the user account from within the controller, after the user is created?
see:
https://github.com/plataformatec/devise/blob/master/lib/devise/models/confirmable.rb
lines 27..30 contain the before_create and after_create hooks
you'll need to do this modification:
you'll need to override :confirmation_required? , so that it returns true
only in the cases where you want a confirmation token to be generated and a confirmation email to be sent.
In the case you don't need the confirmation email, you can do a user.confirm! after creating the user account.
You could put this in as an additional after_create action.
e.g.
module Devise
module Models
module Confirmable
after_create :confirm! , :if => :confirmation_not_required? # you'll need to define that method
private
def confirmation_required? # overriding the default behavior
your_special_conditions && !confirmed?
end
def confirmation_not_required?
! confirmation_required?
end
end
end
end
Note:
Instead of user.confirm! you could also use user.skip_confirmation!