且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Rails Devise:设置密码重置令牌和重定向用户

更新时间:2022-02-02 18:16:13

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

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.