Rails Devise:设置密码重置令牌并重定向用户
在我的应用程序中,针对特定用例,我创建一个新用户(以编程方式设置密码)并向他们发送一封确认电子邮件。
我希望他们能够在确认后立即更改密码(无需输入我不想发送给他们的系统生成的密码)
实际上我希望
1) 系统创建一个新的用户帐户并生成密码。
2) 系统发送确认邮件。
3)用户单击确认并被重定向以输入密码(有效地将他们发送到如下所示的 URL)
<a href="http://localhost:3000/users/password/edit?reset_password_token=v5Q3oQGbsyqAUUxyqLtb">Change my password</a>
任何帮助/指示都会很棒。
In my app for a certain use case I create a new user (programmatically set the password) and send them a confirmation email.
I would like them to be able to change their password immediately after confirming (without having to enter the system generated one which I don't want to send them)
In effect I would like
1) System creates a new user account with generated password.
2) System sends confirmation email.
3) User clicks confirmation and is redirected to enter in their password (effectively send them to a URL like below)
<a href="http://localhost:3000/users/password/edit?reset_password_token=v5Q3oQGbsyqAUUxyqLtb">Change my password</a>
Any help / pointers would be great.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
一种简单的方法,让用户只需一步即可使用您建议的链接确认电子邮件地址并设置初始密码...
发送您的应用程序生成的一封电子邮件,包括重置密码令牌,并考虑用户拥有该令牌,确认该令牌的有效性电子邮件。
在系统帐户生成代码中,假设用户模型设置有 :recoverable 和 :database_authenticatable 设计模块...
在设置初始密码时,使设计重置密码视图对用户来说更清晰。
view/devise/passwords/edit.html.erb
生成的电子邮件
无需在您的用户模型中包含 :confirmable Devise 模块,因为如果电子邮件中没有 reset_password_token,则无法访问您的应用程序创建的帐户。
Devise 将处理提交并清除 Reset_password_token 字段。
有关
reset_password_token
方法和朋友的详细信息,请参阅devise_gem_folder/lib/devise/models/recoverable.rb
和database_authenticable.rb
。如果您想使用 Devise
:confirmable
模块而不是这种方法,请参阅 设计 wiki 页面。A simple way to have just one step for users to confirm email address and set initial password using the link you proposed...
Send one email your app generates, including a reset_password_token, and consider user's possession of that token confirmation of the validity of that email address.
In system account generation code, assuming User model is set up with :recoverable and :database_authenticatable Devise modules...
Make the devise reset password view a little clearer for users when setting initial password.
views/devise/passwords/edit.html.erb
Generated Email
No need to include :confirmable Devise module in your User model, since accounts created by your app won't get accessed without the reset_password_token in the email.
Devise will handle the submit and clear the reset_password_token field.
See
devise_gem_folder/lib/devise/models/recoverable.rb
anddatabase_authenticatable.rb
for details onreset_password_token
method and friends.If you want to use Devise
:confirmable
module rather than this approach, see the Devise wiki page.在 Rails 4.1 中,对 Anatortoise House 回复的以下修改有效:
电子邮件视图有此链接:
In Rails 4.1, the following modification of Anatortoise House's reply works:
The email view has this link:
这是我的邮件预览片段
Here is my snippet for mailer preview
您可以调用
它可能不稳定,因为它是受保护的方法,但它可以适合您的情况。只需用测试覆盖它即可。
(在 Devise v. 3.4 上测试)
You can call
It may not be stable, as it's a protected method but it can work for your case. Just cover it with a test.
(tested on Devise v. 3.4)