3
votes

I am trying to turn #registerable off in devise and just register users through console and seed data however I am having an issue with sign in.

  1. I created a user via the console: User.create!(email: '[email protected]', password: 'password', username: 'username')
  2. It passes validation and gets saved to the database
  3. I cannot sign in (through the website form) with this user when I provide the email: [email protected] and password: password
  4. This is the password though, in console:

    u = User.find_by_email('[email protected]')
    
    u
     => #<User id: 6, email: "[email protected]", encrypted_password: "$2a$10$xd0PMJ.qDDu6VtFd6i2EM.bQ0h1rvODyzaqFdzo3Uo4C...", username: "user", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: "2013-09-30 01:25:16", updated_at: "2013-09-30 01:25:16">
    
    
    u.valid_password? 'password'
     => true
    

I've tried multiple users and I've tried creating it with db/seed.rb. Again, it does get into the database and everything looks fine. It just won't sign in. I've also tried adding the password_confirmation field with a matching password, no dice.

If I turn on :registerable in the user model, goto the sign up page, sign a user up, I can log in with that user (created through the form).

My only thought is that there is some kind of difference in the way the encrypted password is being generated? But that does not hold because I can run the same find_by_email/valid_password check on the web created user.

Any help would be greatly appreciated?

Edit:

I am not using Confirmable, here is my Devise attributes:

     devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatablen

Edit2 Now I can not login with my web created accounts. I need to do more debugging.

2
Please explain what you're seeing when you attempt to sign inRyan Bigg
It re-renders the users/sign_in page with the flash header set: "Invalid email or password." 401 error in Rails Server logMike
What happens if you create a user in console, then another with the same password via browser? Do the encrypted passwords match?Chris Peters

2 Answers

3
votes

Assuming your are using Confirmable, use the following to create the user:

User.create!(email: '[email protected]', password: 'password', username: 'username', confirmed_at: Time.now)

Otherwise, devise will wait for the user to confirm their email address which never happens

3
votes

I solved this problem. I made a misconfiguration in the devise initializer. I mis-read the config.authentication_keys line and thought that it was either/or for keys and not all of the keys. If another person found this by google, I recommend returning to the auto-generated initializer or git roll back to it. I don't think this had anything to do with the difference in user creation or :registrable module of devise.