Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Devise Completed 401 Unauthorized in 1ms after apply strong parameter

I just configured strong parameter in my app, all seems to be fine. But my devise login failed. I can still register a user and that will login the user in. I am on devise 3.1.1

I added to application_controller:

   before_filter :configure_permitted_parameters, if: :devise_controller? 

     protected
      def configure_permitted_parameters
        devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:email, :password, :remember_me) }
        devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:username, :email, :password) }
      end

In my model

   devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable, :token_authenticatable, :invitable, :invite_for => 2.weeks, :authentication_keys => [:username]

And my log, there is a DEPRECATION WARNING: devise :token_authenticatable is deprecated., not sure if this is the problem?

       Started POST "/users/login" for 127.0.0.1 at 2013-10-09 21:54:13 +1300
      DEPRECATION WARNING: devise :token_authenticatable is deprecated. Please check Devise 3.1 release notes for more information on how to upgrade. (called from <class:User> at /home/jcui/Desktop/workspace/iv/app/models/user.rb:6)
        Configuration Load (0.4ms)  SELECT "configurations".* FROM "configurations" 
      Processing by Devise::SessionsController#create as HTML
        Parameters: {"utf8"=>"✓", "authenticity_token"=>"G1aVfzHcwHAI7ao6sBLF9WtgJAWlQ8c5KlKzEHpZzTo=", "user"=>{"email"=>"[email protected]", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Sign in"}
      Completed 401 Unauthorized in 1ms
      Processing by Devise::SessionsController#new as HTML
        Parameters: {"utf8"=>"✓", "authenticity_token"=>"G1aVfzHcwHAI7ao6sBLF9WtgJAWlQ8c5KlKzEHpZzTo=", "user"=>{"email"=>"[email protected]", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Sign in"}
        Rendered devise/shared/_links.erb (0.6ms)
        Rendered devise/sessions/new.html.erb within layouts/application (6.8ms)
      Completed 200 OK in 169ms (Views: 83.0ms | ActiveRecord: 0.0ms | Solr: 0.0ms)

I tried to output the resource error object in the session new view, but there is not error!!

#<ActiveModel::Errors:0xe911424 @base=#<User id: nil, email: "[email protected]", encrypted_password: "$2a$10$j6lAQhBNgsO01HuBjxbgCOdvd0biRehWhQct50ee8cAo...", 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: nil, updated_at: nil, api_key: nil, avatar: nil, deleted_at: nil, roles_mask: nil, subdomain_id: nil, 
  invitation_token: nil, invitation_sent_at: nil, invitation_accepted_at: nil, invitation_limit: nil, invited_by_id: nil, invited_by_type: nil, username: nil, avatar_processing: nil, lang: nil>, @messages={}>
like image 430
user1883793 Avatar asked Nov 12 '22 20:11

user1883793


1 Answers

You are using the username as the mode of authentication in your devise model

:authentication_keys => [:username]

where as in your sign-in,you have email for authentication. Try changing your permitted parameters from

 devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:email, :password, :remember_me) } 

to

devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:username, :password, :remember_me) }

Also modify config/initializers/devise.rb to have:

config.case_insensitive_keys = [ :email, :username ]
config.strip_whitespace_keys = [ :email, :username ]

Check out this link if you want to use either username or password for login: https://github.com/plataformatec/devise/wiki/How-To:-Allow-users-to-sign-in-using-their-username-or-email-address

like image 82
James Avatar answered Nov 15 '22 04:11

James