DEV Community

loading...

How to manually change the password using Devise

Toshiharu Nishina
A web developer for the fifth year.Rails/React/GraphQL.
Updated on ・1 min read

When you create a web application with Rails, using devise makes it easy to create a login authentication part.

By using devise, the user's password is encrypted and stored.

There are times when you want to change your password by directly modifying data as you are developing.
In that case, do as follows.

user = User.find(ID of the user whose password you want to change)
user.password = 'new password'
user.password_confirmation = 'new password'
user.save
Enter fullscreen mode Exit fullscreen mode

by @nishina555

Discussion (4)

Collapse
citizen428 profile image
Michael Kohl • Edited

Or you can use Devise::Recoverable#reset_password:

 User.find(id).reset_password(password, password)
Enter fullscreen mode Exit fullscreen mode

If you're not using Devise::Recoverable, you can at least assign both passwords in one go:

user.password = user.password_confirmation = 'new password'
Enter fullscreen mode Exit fullscreen mode
Collapse
nishina555 profile image
Toshiharu Nishina Author • Edited

I see!! Thank you for your comment.

Collapse
nishina555 profile image
Toshiharu Nishina Author

You are right!
We may not need to create the filed of confirming a password to forms.
Even if there is the field, it may be useless because many users put it just by copying and pasting the password.

I wrote the post because there was the field of confirming the password in the web service which I develop.

I saw Airbnb's website, and I wasn't asked to confirm the password.

In Japan, confirming the password is standard for web service, but it may be getting rid of in near future.