简体   繁体   中英

Migrating existing user model to Devise

I'm trying to migrate a legacy app to Rails 3 and change the authentication to use Devise. I've created the model and migrations and imported all the user data.

I don't plan to migrate the passwords over as the existing scheme is not one we'd like to use going forward, but I want to be able to present users with a simple experience.

Ideally I'd like to catch a login error and then check the password with the legacy field and then update the Devise password with it if it matches.

I can see that Warden gives me a callback that can trap errors so I expect I can trap a login error.

However because all the passwords (in Devise) are blank I get errors relating to the hash as the encrypted_password fields are empty.

Is there a way I can update all the user accounts with a random password?

I've seen in Devise::Models::DatabaseAuthenticatable that there is a method 'password=' but if I call that, eg in rails console for the app:

User.find(1).password=('new')
 => "new"

I just get the same plain text string back ('new') and saving the user record post this doesn't populate the encrypted_password field.

I've searched around but can't seem to be able to find it. Any suggestions much appreciated!

Ok just in case anyone else is as cloth headed as I have been the last 24 hours, here's how you set the password:

user = User.find(id)
user.password = 'new-password'
user.save

Simple really:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM