Rails Devise:设置密码重置令牌和重定向用户 [英] Rails Devise: Set password reset token and redirect user

查看:189
本文介绍了Rails Devise:设置密码重置令牌和重定向用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我创建一个新的用户(以编程方式设置密码)并发送确认邮件。

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)

实际上我想要

1)系统使用生成的密码创建一个新的用户帐户。

2)系统发送确认邮件。

3)用户点击确认并重定向到他们的密码(有效地将它们发送到如下所示的URL)

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.

推荐答案

一个简单的方法,让用户只需一步确认电子邮件地址,并使用您提出的链接设置初始密码。

A simple way to have just one step for users to confirm email address and set initial password using the link you proposed...

发送您的应用程序生成的一封电子邮件,包括一个reset_password_token,并考虑用户对该电子邮件地址的有效性的拥有。

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.

在系统帐户生成代码,假设用户模型设置为:可恢复和:database_authenticatable设计模块...

In system account generation code, assuming User model is set up with :recoverable and :database_authenticatable Devise modules...

acct = User.new
acct.password = User.reset_password_token #won't actually be used...  
acct.reset_password_token = User.reset_password_token 
acct.email = "user@usercompany.com" #assuming users will identify themselves with this field
#set other acct fields you may need
acct.save

使设置初始密码时,设置重置密码对于用户来说可以更清晰一些。

Make the devise reset password view a little clearer for users when setting initial password.

...
<%= "true" == params[:initial] ? "Set your password" : "Reset your password" %>
...  



生成的电子邮件



Generated Email

Hi <%= @user.name %>
An account has been generated for you.
Please visit www.oursite.com/users/password/edit?initial=true&reset_password_token=<%= @user.reset_password_token %> to set your password.

不需要包含:可确认您的用户模型中的设计模块,由于您的应用程序创建的帐户赢得'在电子邮件中没有reset_password_token将被访问。

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将处理提交并清除reset_password_token字段。

Devise will handle the submit and clear the reset_password_token field.

请参阅 devise_gem_folder / lib / devise / models / recoverable.rb database_authenticatable.rb 有关 reset_password_token 方法和朋友。

See devise_gem_folder/lib/devise/models/recoverable.rb and database_authenticatable.rb for details on reset_password_token method and friends.

如果要使用Devise :可确认模块而不是这种方法,请参阅 Devise wiki页面

If you want to use Devise :confirmable module rather than this approach, see the Devise wiki page.

这篇关于Rails Devise:设置密码重置令牌和重定向用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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