Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Devise - Rails 4 - Not able to login

I just started using Rails 4. I got devise setup with custom fields. I'm able to login after I register, but I'm not able to login from the login page. The user registration goes in fine, and I have username, password etc in the database table.

Can somebody tell me what I'm missing? My User model is very basic. Here is how it looks like -

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  validates :username, :uniqueness => true, :presence => {:message => '- Username cannot be blank'}
  validates :first_name, :presence => { :message => " - Firstname cannot be blank!"}    

  private
    def user_params
        params.require().permit(:first_name, :last_name, :username, :email, :password, :password_confirmation)
    end
end

I've checked my view and I'm passing the right parameters.

I have the following in my application controller -

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception

  layout :layout_by_resource

  before_filter :configure_permitted_params, if: :devise_controller?

  def layout_by_resource
    if devise_controller?
        "devise"
    else
        "application"       
    end
  end

  protected

  def configure_permitted_params
    devise_parameter_sanitizer.for(:sign_up) {|u| u.permit(:username, :first_name, :last_name, :password, :password_confirmation, :email) }
  end

end

I checked the database table and I do have username and password (encrypted_password that is). I have my authentication keys set to username.

Not sure what I'm missing here. I have devise 3.0.0

like image 374
Aswin Ramakrishnan Avatar asked Oct 02 '22 23:10

Aswin Ramakrishnan


1 Answers

In your application controller you have permitted params for sign_up, but not for sign_in. So i you change the configure_permitted_parameters method to this, it should work:

def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:username, :password, :remember_me) }
    devise_parameter_sanitizer.for(:sign_up) {|u| u.permit(:username, :first_name, :last_name, :password, :password_confirmation, :email) }
  end
like image 133
John Avatar answered Oct 07 '22 18:10

John