2016-04-29 37 views
0

ユーザー名を使用してDeviseにログインする際に問題が発生しました。電子メールで問題が発生していません。私はすべての指示に従って、私は正常にユーザー名/電子メールでユーザーにサインアップしました。私が受け取っているエラーは以下の通りです。Devise - ユーザー名でログインできませんRails 4

Started POST "https://stackoverflow.com/users/sign_in" for 127.0.0.1 at 2016-04-29 15:09:09 -0400 
Processing by Devise::SessionsController#create as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"RL7IXaRa6OOEValataRLI6x3OIky0bOiEstqzJCvYgM5TTg0N3ydRQTY/FjBNXgPHIkuShp2JECeTfI7OM1aqQ==", "user"=>{"login"=>"jonsnow200", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Log in"} 
    User Load (0.0ms) SELECT "users".* FROM "users" WHERE (username = 'jonsnow200' OR lower(email) = lower('jonsnow200')) ORDER BY "users"."id" ASC LIMIT 1 
Completed 401 Unauthorized in 7ms (ActiveRecord: 1.5ms) 
Processing by Devise::SessionsController#new as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"RL7IXaRa6OOEValataRLI6x3OIky0bOiEstqzJCvYgM5TTg0N3ydRQTY/FjBNXgPHIkuShp2JECeTfI7OM1aqQ==", "user"=>{"login"=>"jonsnow200", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Log in"} 
    Rendered users/sessions/new.html.erb within layouts/application (16.0ms) 
    Rendered navigation/_navbar.html.erb (9.0ms) 
Completed 200 OK in 2339ms (Views: 2229.0ms | ActiveRecord: 0.0ms) 

Application_Controller.rb

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

    def redirect_back_or(path) 
    redirect_to request.referer || path 
    end 

    protected 
    def configure_permitted_parameters 
    devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:username, :avatar, :email, :password, :password_confirmation, :remember_me, :avatar_cache, :remove_avatar) } 
    devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:login, :username, :email, :password, :remember_me) } 
    devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:username, :display_name, :age, :gender, :country_id, :email, :avatar, :avatar_cache, :remove_avatar, :password, :password_confirmation, :current_password) } 
    end 
end 

devise.rb

config.authentication_keys = [:login] 
config.case_insensitive_keys = [:email] 
config.strip_whitespace_keys = [:email] 
config.skip_session_storage = [:http_auth] 
config.stretches = Rails.env.test? ? 1 : 10 
config.reconfirmable = true 
config.confirmation_keys = [:username] 
config.expire_all_remember_me_on_sign_out = true 
config.password_length = 8..72 
config.reset_password_keys = [:username] 
config.reset_password_within = 6.hours 
config.scoped_views = true 
config.sign_out_via = :delete 

user.rb

class User < ActiveRecord::Base 
    attr_accessor :login 
    validate :validate_username 
    friendly_id :username, use: :slugged 
    accepts_nested_attributes_for :profile 
    # Include default devise modules. Others available are: 
    # :confirmable, :lockable, :timeoutable and :omniauthable 
    devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable, :validatable, :token_authenticatable #:encryptable 

    mount_uploader :avatar, AvatarUploader 
    validates :avatar, :presence => true 
    validates_integrity_of :avatar 
    validates_processing_of :avatar 
    validates :username, 
      :presence => true, 
      format: {with: /\A[a-zA-Z0-9]*\z/, message: "may only contain letters and numbers."}, 
      :uniqueness => { 
       :case_sensitive => false, length: {maximum: 16} 
      } 
    validates :email, 
      :presence => true, 
      :uniqueness => { 
       :case_sensitive => false, length: {minimum: 5} 
      } 

    def validate_username 
    if User.where(email: username).exists? 
     errors.add(:username, :invalid) 
    end 
    end 

    def self.find_first_by_auth_conditions(warden_conditions) 
    conditions = warden_conditions.dup 
    if login = conditions.delete(:login) 
     where(conditions.to_hash).where(["username = :value OR lower(email) = lower(:value)", {:value => login}]).first 
    else 
     where(conditions.to_hash).first 
    end 
    end 
end 

答えて

0

私は以来、ユーザー名/電子メールのfinder_methodを交換しなければなりませんでした回復プロセスをusernamにアクセスするように変更しましたes/email

def self.find_first_by_auth_conditions(warden_conditions) 
    conditions = warden_conditions.dup 
    if login = conditions.delete(:login) 
     where(conditions).where(["lower(username) = :value OR lower(email) = :value", { :value => login.downcase }]).first 
    else 
     if conditions[:username].nil? 
     where(conditions).first 
     else 
     where(username: conditions[:username]).first 
     end 
    end 
    end 
関連する問題