Rails 3.1. Create a single user in a password protected console

I want to create a single user (admin), and I want to use the console (without a user registration model). I am using a solution from RailsCasts (http://railscasts.com/episodes/270-authentication-in-rails-3-1). But I have one problem: when I do User.create (...,: password => "pass") in the console, my password is stored in the database without specifying (for example, "pass"). And I can not log in with my data.

How can I create a user from the console? :)

+6
source share
1 answer

Straight from the Rails API

# Schema: User(name:string, password_digest:string) class User < ActiveRecord::Base has_secure_password end user = User.new(:name => "david", :password => "", :password_confirmation => "nomatch") user.save # => false, password required user.password = "mUc3m00RsqyRe" user.save # => false, confirmation doesn't match user.password_confirmation = "mUc3m00RsqyRe" user.save # => true user.authenticate("notright") # => false user.authenticate("mUc3m00RsqyRe") # => user 

You need to include :password_confirmation => "pass in your hash!

That's correct, therefore, looking at has_secure_password , you want to execute BCrypt::Password.create(unencrypted_password) to get it. To accomplish the above, you will need a bcrypt-ruby stone.

+21
source

Source: https://habr.com/ru/post/900221/


All Articles