I need to do something like this from Rails console to do some testing and experiments:
User.authenticate(username, password)
I’m using Devise, but I have no idea how to do this.
I saw this other answer here
How to sign in a user using Devise from a Rails console?
But I need something cleaner and more direct. If necessary, I just need the algorithm to hash an attempted password with the salt and compare it to the encryped_password.
Is it this?
User.find(1).valid_password?('password123')
General info:
Check out the Devise README found here.
As an example, here is a helper method that you can use to do this test:
Testing it, I get:
Also working:
User.find(1).valid_password?('1')in Rails console:For reference, here’s my Devise setup for the User model:
Like you said in the comments, there is no
sessions_controllersince Devise. Look into the Devise config file in/config/initializers/devise.rb. Depending on your application configuration, you may also want to look at thetoken.rbandsession_store.rbinitializer configs.EDIT: Can you check your user model to see if devise is properly configured for it? Compare it against my
deviseline. The:validatableproperty could be missing.EDIT 2: Added the helper method to do this test if you want to do it outside of the console. Check up top.
Side-note: The
rails_admingem is pretty useful and may be worth checking out!Hope that helps.