Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get password reset url in devise?

I am porting my data from old system to a new system written in ruby on rails. To get the older users registered I am planning to transfer their old data into new system but I can't transfer their old password so I'm planning to create a random password and then a password reset link and send them a custom email inviting them to my new system.

Devise provides this: user.send_reset_password_instructions

But this sends a "forgot password" email to user. I just want to get the forgot password url somehow so that I can use that url in my own mail and send it at some later time. I've tried looking up but everywhere they talk about "send_reset_password_instructions" function. Any idea how I can do this?

like image 785
vik-y Avatar asked Mar 15 '16 03:03

vik-y


2 Answers

The reset password url was formed by reset_password_token in User model.

So saving the reset_password_token is enough to recover reset password url later on.

reset_password_token = 'XYZ' # Example token
reset_password_url = Rails.application.routes.url_helpers.edit_user_password_path(reset_password_token: reset_password_token)
like image 72
Hieu Pham Avatar answered Nov 10 '22 22:11

Hieu Pham


Due to security (an attacker could read the link from the database to bypass email verification), what Devise stores in user.reset_password_token is only a digest of the token that is sent into the password reset link.

Specifically, in the set_reset_password_token method, the encoded token is saved to the database, while the raw token is returned and then sent in the password reset email.

What you can do is to reset the token yourself and save the raw token somewhere to be used later:

raw = user.send(:set_reset_password_token)

It's also worth noting that you can customize the devise mailer and provide your own templates. However, in this case, it would also affect the legitimate password reset emails.

like image 38
Petr Bela Avatar answered Nov 10 '22 23:11

Petr Bela