开发者

RoR devise: sign_in always returns invalid email/password

开发者 https://www.devze.com 2023-04-05 09:14 出处:网络
Every time I log in, I get the error msg that the email/password is invalid. routes: devise_for :users devise_scope :users do

Every time I log in, I get the error msg that the email/password is invalid.

routes:

devise_for :users

devise_scope :users do
  get '/users/sign_out' => 'devise/sessions#destroy', :as => :destroy_user_session
  post '/users/sign_in' => 'devise/sessions#create', :as => :user_session
end

resources :users

user model:

devise :database_authenticatable, :confirmable, :recoverable, :rememberable, :trackable

attr_accessor :password
attr_accessible :first_name, :last_name, :email, :password, :password_confirmation, :gender, :remember_me

view:

<% if signed_in?(:user) %>
   Hi <%= current_user.first_name %>. | Not you? <%= link_to 'Sign out', destroy_user_session_path, :method => :delete %>
<% else %>
   <%= link_to 'Sign up', signup_path %> or <%= link_to 'Sign in', user_session_path, :method => :create %>
<% end %>

I tried changing the routes to:

get '/users/sign_in' => 'devise/sessions#new', :as => :new_user_session

and changing the respective paths, but that didn't change anything.

I even changed the code in the view from:

if signed_in?(:user)

to:

if user_signed_in?

and did a combination of these things and nothing is working.

I did ask devise to handle the confirmation as well, which I did by copying the generated link to the browser and it signs me in as the user the first time. It also allows me to change the password via the email confirmation link, which also signs me in upon changing the password. But once I sign out and sign back in, it tells me that the email/password is invalid again.

Can anyone help?

I am using rails 3.0.7, devise 1.4.5, capybara 1.1.1, cu开发者_JS百科cumber 1.0.6, mysql2 0.2.6 and rake 0.8.7 if that helps anyone.

Thanks

EDIT:

To help future users, there is actually nothing wrong with the gem. It works fine. The issue is with my database. For some reason, it is selecting a NULL email from the database instead of pulling the info of the user I am trying to log in. I am figuring out how to fix this now and will update once I figure it out.


I have been having a similar issue on a fresh install of Devise, but what I found is that if I ran bundle update then restarted my dev server it works.

At first I wasn't seeing the DB query in the development.log, but once I updated my Gemfile (to make sure I am using the latest Devise gem) and restarted the server I now see the db queries in my logfile and it works (magically).

Hope that helps.

Btw, not sure if this will help you...but these are the routes in my Routes.rb

 devise_for :users, :path_names => { :sign_up => "register", 
                                      :sign_in => "login", 
                                      :sign_out => "logout",
                                      :settings => "settings" }

  devise_scope :user do
    get "login", :to => "devise/sessions#new"
    get "register", :to => "devise/registrations#new"
    get "settings", :to => "devise/registrations#edit"
    get "logout",   :to => "devise/sessions#destroy"

  end


I ran into this issue when I had both

config.authentication_keys = [ :username ]

and

config.authentication_keys = [ :email ]

enabled simultaneously in config/initializers/devise.rb.


Upon removing the duplicate setting, authentication was successful.


When I ran across this problem I went back through my Devise config file in

config/initializers/devise.rb

I then found the part that I remembered I had changed to :username

config.authentication_keys = [ :username ]

I changed this back to :email. That ended up making it work.


I think the attr_accessor messes up with attr_accessible. Do you really need to have an attr_accessor?, if not simply remove it.

Try it and please let me know how it goes.

Best regards

ED


The username and password could both be case sensitive. Have you checked how the username is stored in your table to make sure you are entering it correctly?

In my experience when it tells you the email/password is incorrect, you can get distracted with lots of complicated ideas, but it ends up being the email or password was incorrect. It sounds like you have tried various times with resetting the password, so it makes me wonder about the username. Are you sure the right field is set up in devise as the username (ie. it could be email or a username)?


config.authentication_keys = [ :username ]

and

config.authentication_keys = [ :email ]

simultaneously set in config/initializers/devise.rb will cause this error.

0

精彩评论

暂无评论...
验证码 换一张
取 消