Thursday 14 November 2013

Hashing password as devise do

I am stuck in an stupid office waiting for some seal, so in order to not to lose all my day I guess I would write some tip, many of this tips are for my future myself because I hate when I've spent two hours resolving a problem and half year later I need start searching all over again, sometimes is just a parameter in a command sometimes a lib I need to compile emacs. If you are a developer you know what I'm talking about ;-)

The problem: sometimes I just wanna update a password manually using PostgreSQL console or sometimes I wanna create the initial user settled in seeds.rb file, so to do this I need hash the password first. We do this entering in the rails console:

$ bundle exec rails c RAILS_ENV=development

And now the hash:

hashed_password = ::BCrypt::Password.create("s0m3HardAndNewP44ss")

Update the user model:

user = User.find(456)
user.update_attribute('encrypted_password', hashed_password)

Or you can do it in the old SQL way:

UPDATE users SET encrypted_password='$2a$10$4R.gf6j9AmV4GAgszYVLxeCa' WHERE id=456;

And that is all!!