如何生成重置密码令牌 [英] How to generate reset password token

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

问题描述

我正在使用devise gem进行身份验证。



在我的应用程序中,管理员将创建用户,所以在管理员创建用户时,我想要用户的重置密码链接。 p>

这是我的操作: -

  def create 
@ user = User.new(user_params)
@ user.password ='123123123'
@ user.password_confirmation ='123123123'
if @ user.save
@ user.update_attributes :confirm_token => nil,:confirmation_at => Time.now,:reset_password_token =>(0 ... 16).map {(65 + rand(26))。chr} .join,:reset_password_sent_at => Time.now)
UserMailer.user_link(@user).deliver
redirect_to users_path
else
render:action => new
end
end

这是我的链接,密码





但是当我打开链接并更新密码时,我正在获得重置密码令牌是无效的。

解决方案

可以使用 user.send_reset_password_instructions


I am using devise gem for authentication.

In my application admin will create the users, so I want the user's reset password link when admin creates users.

This is my action:-

def create
   @user = User.new(user_params)
   @user.password = '123123123'
   @user.password_confirmation = '123123123'
   if @user.save
         @user.update_attributes(:confirmation_token => nil,:confirmed_at => Time.now,:reset_password_token => (0...16).map{(65+rand(26)).chr}.join,:reset_password_sent_at => Time.now)
       UserMailer.user_link(@user).deliver
       redirect_to users_path
     else
    render :action => "new"
   end
end

This is my link to reset a user's password

But I am getting reset password token is invalid when I open the link and update the password.

解决方案

You can use user.send_reset_password_instructions for that.

这篇关于如何生成重置密码令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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