简体   繁体   中英

Rails3.2 - How to customize Devise to generate random password when a user signs up?

Im trying to customize Devise registration process to generate a random password and sends it with the confirmation mail.

What I have already done is to override the default Devise's :validtable and to generate a new random password if needed.

  before_validation :password_generation

  def password_generation
       password_confirmation = password = Devise.friendly_token.first(7) if password.nil? || password.blank?
  end

Now my problem is to include the newly generated random password with the original confirmation mail.

Is there any possibility to keep up with the original usability of Devise while customizing it's new user process or should I build the authentication process from scratch ?

Thanks, Hadar.

(four months too late, but perhaps someone else can use this)

Mostly what you need to do is copy the default Devise mailer and views to your project and in the config/initializers/devise.rb specify your local class in config.mailer . At that point, you can customize the email as you like. (note that Devise uses the term resource as an abstraction, but mostly it means an instance of User).

The only trick will be to find a way to remember the generated password between the time that the new user account is created and when the email is generated and sent. What Devise will store, when saving the user record is an encrypted version of the password. I would have to follow the path of logic in Devise to be sure, but I'll bet you could store it in an instance variable on User, perhaps params or maybe the flash. Be very careful about this; you want this value to be as short-lived as possible -- presumably just the lifetime of the request.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM