用设计确认页面显示用户密码 [英] Displaying User Password With Devise Confirmation Page

查看:110
本文介绍了用设计确认页面显示用户密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Devise邮件发送的确认页面中显示用户密码。确认页面是默认的

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

但是,我希望有

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)?

我注意到@email和@resource在视图中使用。我可以使用其中任何一个以未破译的形式访问当前密码?

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?

请注意,我正在使用 user.find( 1).send_confirmation_instructions

推荐答案

尽管可以做到这一点,我会非常强烈地警告反对这样做。专门使用哈希密码,以便无法轻松重新创建密码。将原始密码传回用户将导致以纯文本的方式发送,这样会导致整个目的的失败。此外,用户不应该知道他们的密码(他们确实输入了两次)?!?

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)?!?

为此,您需要捕获原始(unhashed)密码注册创建操作并发送电子邮件在那一点(传递密码)。您可以通过覆盖 sign_up 方法来执行此操作 - 您可以在初始化程序中执行此操作:

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

或者,您可以从 Devise :: RegistrationsController派生一个新的控制器并把这个覆盖代码放在那里(推荐的方法 - 但是再一次,这个整个操作并不是真的推荐)。您需要添加 unhashed_pa​​ssword 访问器,以使其正常工作:

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

然后你可以更新您的确认视图(位于 app / views / devise / mailer / confirmation_instructions.html.erb )以包含以下内容:

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>

这篇关于用设计确认页面显示用户密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆