简体   繁体   中英

Rails: Devise gem password encryption

I am working on a feature, in which I must update all the account details in the database manually. It is working fine but the only issue is, Since we have also using the devise gem for the login feature. I must store the encrypted format of the password I am updating manually. So I just want to know how the devise gem encrypts a password.

If my password is "password". I must manually encrypt this and store it in the database in the same way of the devise.

Please revert back for the help asap.

I'm assuming you have a list of usernames/passwords which you'd like to mass-assign into a database for use in an app that authenticates via Devise.

Devise by default uses BCrypt, (there is a gem)

require 'bcrypt'

class User < ActiveRecord::Base
  # users.password_hash in the database is a :string
  include BCrypt

def password
  @password ||= Password.new(password_hash)
end

def password=(new_password)
  @password = Password.create(new_password)
  self.password_hash = @password
end

end

You can create instances of the users in your list and save to the database in a Rake task, for example.

@user = User.new
@user.username = "foobar"
@user.password = "password"
@user.save

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