Iain
Iain

Reputation: 972

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, e.g. 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!

Upvotes: 1

Views: 1042

Answers (1)

Iain
Iain

Reputation: 972

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 :)

Upvotes: 2

Related Questions