Devise: manually encrypt password and store directly

前端 未结 5 939
[愿得一人]
[愿得一人] 2021-02-01 02:27

I\'m trying to migrate a ton of users from an old database. To do this, I\'m using activerecord-import and trying to save all my user data directly to DB (bypassing the User mo

5条回答
  •  日久生厌
    2021-02-01 03:14

    You should do it like this:

    password = 'the secret password'
    new_hashed_password = User.new(:password => password).encrypted_password
    

    This is much better than using BCrypt directly as it abstracts away how passwords are generated from your code, making it easier to understand, and also immune to changes in how devise constructs encrypted passwords. Your code should not, and has no reason to know anything about that.

提交回复
热议问题