Displaying User Password With Devise Confirmation Page

devise, ruby-on-rails, ruby-on-rails-3

Solution

Although this can be done, I would caution very strongly against doing so. Hashed passwords are specifically used so that the password cannot be recreated easily. Passing the original password back to the user will cause it to be sent back in plain text which sort of defeats the whole purpose. Also, shouldn't the user already know their password (they did type it in twice after all)?!?

To do this, you would need to capture the original (unhashed) password in the registration `create` action and send the email at that point (passing along the password). You can do this by overriding the `sign_up` method - you can do this in an initializer:

class Devise::RegistrationsController < DeviseController
  def sign_up(resource_name, resource)
    sign_in(resource_name, resource)
    resource.unhashed_password = resource_params[:password]
    resource.send_confirmation_instructions
  end
end

Alternatively, you can derive a new controller from `Devise::RegistrationsController` and put this override code there (the recommended approach - but then again, this whole operation isn't really recommended). You'll need to add the `unhashed_password` accessor for this to work:

class User < ActiveRecord::Base
  attr_accessor :unhashed_password
end

And then you can update your confirmation view (at `app/views/devise/mailer/confirmation_instructions.html.erb`) to contain this:

<p>Your password is currently <%= @resource.unhashed_password %></p>

Problem

I'm attempting to display a users password along in his confirmation page sent by the Devise mailer. The confirmation page is the default ``` Welcome test0@test.com! You can confirm your account email through the link below: Confirm my account ``` However, I wish to have ``` Welcome test0@test.com! Your password is currently DASADSADS You can confirm your account email through the link below: Confirm my account ``` How do I access the user object in the view? Do I need to override the mailer controller with a custom one? If so, how do I tell what the methods of the current mailer do (tried looking at documentation but can't find any clues)? I noticed that @email and @resource are used in the view. Can I use any of these to access the current password in its unhashed form? Note that I am sending this email manually with `user.find(1).send_confirmation_instructions`

Original source