While running an app how do you select a user by email address and then set the password manually within rails console for Devise?
Also, where would I go to review documentation to cover more details in this regard to manipulation of accounts while using Devise?
Modern devise allows simpler syntax, no need to set the confirmation field
user.password = new_password; user.save
# or
user.update(password: new_password)
# $ rails console production
u=User.where(:email => 'usermail#gmail.com').first
u.password='userpassword'
u.password_confirmation='userpassword'
u.save!
If you run the following in the rails console it should do the trick:
User.find_by(email: 'user_email_address').reset_password!('new_password','new_password')
http://www.rubydoc.info/github/plataformatec/devise/Devise/Models/Recoverable
You can simply update password field, no need for confirmation password, devise will save it in encrypted form
u = User.find_by_email('user#example.com')
u.update_attribute(:password, '123123')
For some reason, (Rails 2.3??)
user = User.where(:email => email).first
didn't work for me, but
user = User.find_by_email('user#example.com')
did it.
1.Login in to ralis console
$ sudo bundle exec rails console production
2.Then update the administrator's password
irb(main):001:0> user = User.where("username = 'root'")
irb(main):002:0> u = user.first
irb(main):003:0> u.password="root2014#Robin"
=> "root2014#Robin"
irb(main):004:0> u.password_confirmation="root2014#Robin"
=> "root2014#Robin"
irb(main):005:0> u.save
=> true
irb(main):006:0> exit
3.Refresh the login page, use the new password to login, enjoy!
Good Luck!
User.find_by_email('joe#example.com').update_attributes(:password => 'password')
If your account is locked from too many login attempts, you may also need to do:
user.locked_at = ''
user.failed_attempts = '0'
user.save!
Related
I'm working on a Rails Engine which will depend on Devise.
I created an installer using Rails generator which you can find here. The installer creates the first credentials in order to access the administration panel later.
The problem is that I cannot sign in with those credentials. Weirdly then, if I delete the account and recreate it via Rails console, they do work. I think that's due to something happening during the installer.
Here a snippet of it:
# Binda installer generator ...
def setup_devise
return if Rails.env.production?
# Copy the initilializer on the application folder
template 'config/initializers/devise.rb'
# Add secret key
inject_into_file 'config/initializers/devise.rb', after: "config.secret_key = '" do
SecureRandom.hex(64)
end
# Add pepper
inject_into_file 'config/initializers/devise.rb', after: "config.pepper = '" do
SecureRandom.hex(64)
end
# some other code for mailer ...
end
def create_credentials
#username = ask("What's your email? ['mail#domain.com']").presence || 'mail#domain.com'
#password = ask("What's your password? ['password']").presence || 'password'
Binda::User.create( email: #username, password: #password, password_confirmation: #password )
# Binda installer generator continue ...
Here the full code.
More info
I found out that even if I modify devise.rb file the new configuration is not loaded until the end of the installer. This means the new salt/pepper isn't considered while creating the first user. How can I reload it before running the create_credentials method?
Sorted!
The problem is that if you add salt/pepper configuration to the devise.rb of your application you need to reload the environment before being able to create a user with the new settings.
To do that I moved the user creation into a task. This way the installer to reload the environment (and Devise config as weel) and is able to set the encrypted password correctly.
Installer
# lib/generators/myEngine/install/install_generator.rb
def create_credentials
rake 'binda_create_initial_user'
end
Task
# lib/tasks/binda.rake
desc "Create initial user"
task :binda_create_initial_user => :environment do
username = 'mail#domain.com'
password = 'password'
Binda::User.create!( email: username, password: password, password_confirmation: password )
end
If you want to keep asking for mail and password:
# lib/tasks/binda.rake
desc "Create first user"
task :binda_create_initial_user => :environment do
STDOUT.puts "What is your email? [mail#domain.com]"
username = STDIN.gets.strip
username = 'mail#domain.com' if username.blank?
STDOUT.puts "What is your password? [password]"
password = STDIN.gets.strip
password = 'password' if password.blank?
Binda::User.create!( email: username, password: password, password_confirmation: password )
end
I'm trying to use Cucumber in a Rails 4. I've added the Cucumber-Rails gem, followed the steps in the instructions but when I wrote step definitions like so:
When(/^I submit a sign up with the following:$/) do |table|
user = User.create({
first_name: 'Name',
last_name: 'Last',
email: 'email#example.com',
domain: 'example.com',
password: 'foobar',
password_confirmation: 'foobar'
})
end
I get the following error: uninitialized constant User (NameError)
./features/step_definitions/users/sign_up_steps.rb:2:in/^I submit a sign up with the following:$/'
features/users/sign_up.feature:4:in When I submit a sign up with the following:'
What am I missing?
Please check , it's looking like you have missed the User model
User model is missing for which you are running tests.
I'm trying to use capybara on a ruby on rails application to do some content testing, as well I'm using the devise gem to implement user authentication. I'm having trouble logging into my application to perform my test cases.
Initially, my scenario was as follows:
scenario "User arrives at main page" do
visit "purchase_orders#index"
page.should have_content("All")
# some more tests and checks
end
Where purchase_orders#index is the authenticated root, where a user's purchase orders are shown.
But when I was running the tests, I was getting the following error :
expected to find text "All" in "Log in to manage your orders * Email * Password Forgot your password? Remember me Sign up • Didn't receive confirmation instructions? About Us • Contact Us •
which tells me that its not getting past the log in page. I next tried adding the following to my scenario, before running the tests, to make it log in:
visit "purchase_orders#index"
fill_in('Email', :with => 'username#gmail.com')
fill_in('Password', :with => 'password')
click_button('Log in')
where username and password are actual created accounts, but again it fails and doesn't get past the sign in page. Finally, I tried adding a before(:each) method, as follows, to attempt to sign users in for test cases:
before(:each) do
visit "users/sessions#new"
fill_in('Email', :with => 'nico.dubus17#gmail.com')
fill_in('Password', :with => 'password')
click_button('Log in')
end
which, again, did not work for me. So my question is: What is the best practice and syntax for getting past the sign in page, and into the actual application? I've looked for documentation and answers on this, but haven't found anything.
Thank you!
Found an answer. I installed the warden gem to (gem 'warden') and factory girl gem (gem "factory_girl_rails", "~> 4.0") and ran a bundle install. I then created a user fixture with factory girl as follows in a factory.rb file in the spec folder:
# This is a user factory, to simulate a user object
FactoryGirl.define do
factory :user, class: User do
first_name "John"
last_name "Doe"
email "nico_dubus#hotmail.com"
encrypted_password "password"
end
end
in my rails helper file, I added this line to be able to use FactoryGirl's methods without calling it on the class every time:
config.include FactoryGirl::Syntax::Methods
Afterwards, I added these lines to the top of my capybara test page:
include Warden::Test::Helpers
Warden.test_mode!
Finally, I built a user object to use within the scope of the test:
user = FactoryGirl.build(:user)
And whenever I need to log in, I use warden's log in method
login_as(user, :scope => :user)
And voila!
I tried to do this from my seeds.rb:
user = User.find_or_create_by email: ENV['ADMIN_EMAIL'].dup, password: ENV['ADMIN_PASSWORD'].dup, password_confirmation: ENV['ADMIN_PASSWORD'].dup
I am getting this error message:
PG::UndefinedColumn: ERROR: column users.password does not exist
LINE 1: ...sers" WHERE "users"."email" = 'abc#test.com' AND "users"."p...
I tried encrypted_password, but then I get the same for users.encrypted_password_confirmation does not exist.
Thoughts on the best way to do this?
I would just go into the Rails console (rails c)
user = User.new(email: "myemail.com", password: "superSecret")
user.save
You can also change it
user = User.find_by_email("myemail.com")
user.password = "newSuperSecret"
user.save
EDIT:
I just put my first block of code in my seed.rb and ran rake db:seed with no problem. So I think the crux of your problem is just having the password attribute in your find_or_create_by call. I think if you put this in your seed.rb you'll get the results you want (although I have to admit I'm not certain about the environment variables working, you might have to retool that portion of it).
user = User.find_or_create_by(email: ENV['ADMIN_EMAIL'].dup)
user.password = ENV['ADMIN_PASSWORD'].dup if user.encrypted_password.blank?
user.save!
I have successfully installed and configured devise with an admin model with "option 1" of this instruction.
After installing rails_admin and visiting localhost:3000/admin I meet a login page wich is asking for an email and password. Since I have not set an admin email/password I am essentially locked out of rails_admin.
How can I register an admin email/password with rails_admin?
run on command prompt: rake db:seed then you can access with email admin#admin.com and password: "administrator".
I followed #Ganeshkunwar advice and ran rake db:seed but added the following to my seeds.rb
admin_model = RailsAdmin::AbstractModel.new(Admin)
admin_model.new(:email => '****#****.com', :password => '****', :password_confirmation => '****').save
Thanks SO!