Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Migrate users from one Rails app to another, both using Devise, while maintaining passwords

I have fully rebuilt a new Rails app but I need to copy the users over from the old app. Both apps use Devise, but I am not sure how to safely copy the encrypted password and have it work on the new app. Thanks.

like image 273
spitfire109 Avatar asked Sep 27 '22 07:09

spitfire109


1 Answers

This is a relatively old topic, but I recently had a similar issue when migrating users from an app to another, both using Devise as auth system.

As discussed above, a valid option is to extract the auth-related fields (email and encrypted_password) from the previous model and insert them in the new one. You'll just have to make sure that you skip the Devise validation steps when populating the new database, otherwise it will require you to provide a valid password.

Therefore this should work :

user = User.new(email: '[email protected]', encrypted_password: 'existing_password_hash')
user.save(validate: false)

If you have a test user with a known password, you can test the results with :

user.valid_password?('my_known_password')

Hope this helps

like image 82
jean-baptiste Avatar answered Dec 27 '22 09:12

jean-baptiste