Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I properly migrate from has_secure_password to encrypted_password (as both use password_digest) without losing old passwords?

Migrating from has_secure_password to devise causes the following error in the console when interacting with user objects:

.rvm/gems/ruby-2.4.1/gems/devise-4.4.0/lib/devise/models/database_authenticatable.rb:166:in `password_digest'

I understand this is because devise uses the pasword_digest function and so it is incompatible with the password_digest column used by active record's has_secure password.

A solution is to delete the password_digest column from the db but I do not want to loose existing users' passwords.

Should I delete the encrypted_password column devise created and then do a migration to rename password_digest to encrypted_password and then update existing user's passwords or is there a more appropriate solution?

like image 890
Ayrad Avatar asked Jan 22 '18 04:01

Ayrad


1 Answers

1> Rename the column password_digest to encrypted_password.

2> In devise initializer in config/initializers/devise.rb set

config.stretches = 11 # this is default

3> bcrypt is the default hashing or encryption algorithm(so no change needed).

See devise config template.

like image 178
Sachin Singh Avatar answered Sep 22 '22 17:09

Sachin Singh