设计注册成功,登录失败

Posted

技术标签:

【中文标题】设计注册成功,登录失败【英文标题】:Devise sign-up successful, login unsuccessful 【发布时间】:2016-12-01 16:38:14 【问题描述】:

正如标题所说,我使用设计中的用户名和密码字段正确注册。但是,当我尝试登录时,会显示 'Invalid Email, Login or password'。用户名存储在 rails 控制台上,但密码未加密或未在此处显示:

=> #<ActiveRecord::Relation [#<User id: 9, email: "", created_at: "2016-12-01 16:32:20", updated_at: "2016-12-01 16:32:20", username: "benjamin">]>

应用控制器:

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
  before_action :authenticate_user!
  before_action :configure_permitted_parameters, if: :devise_controller?

  protected

    def configure_permitted_parameters
    added_attrs = [:username, :email, :password, :password_confirmation, :remember_me]
    devise_parameter_sanitizer.permit :sign_up, keys: [:username, :password]
    devise_parameter_sanitizer.permit :account_update, keys: [:username, :password]
  end


end

用户.rb:

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  has_many :stories
  validates :username, :presence => true, :uniqueness =>  :case_sensitive => false
  validates_format_of :username, with: /^[a-zA-Z0-9_\.]*$/, :multiline => true
  attr_accessor :login
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :authentication_keys => email: true, login: false

 def self.find_for_database_authentication(warden_conditions)
      conditions = warden_conditions.dup
      if login = conditions.delete(:login)
        where(conditions.to_h).where(["lower(username) = :value OR lower(email) = :value",  :value => login.downcase ]).first
      elsif conditions.has_key?(:username) || conditions.has_key?(:email)
        where(conditions.to_h).first
      end
    end

def email_required?
  false
end

def email_changed?
  false
end

end

我的数据库文件相关:

class DeviseCreateUsers < ActiveRecord::Migration[5.0]
  def change
    create_table :users do |t|
      ## Database authenticatable
      t.string :email,              null: false, default: ""
      t.string :encrypted_password, null: false, default: ""

      ## Recoverable
      t.string   :reset_password_token
      t.datetime :reset_password_sent_at

      ## Rememberable
      t.datetime :remember_created_at

      ## Trackable
      t.integer  :sign_in_count, default: 0, null: false
      t.datetime :current_sign_in_at
      t.datetime :last_sign_in_at
      t.string   :current_sign_in_ip
      t.string   :last_sign_in_ip

      ## Confirmable
      # t.string   :confirmation_token
      # t.datetime :confirmed_at
      # t.datetime :confirmation_sent_at
      # t.string   :unconfirmed_email # Only if using reconfirmable

      ## Lockable
      # t.integer  :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
      # t.string   :unlock_token # Only if unlock strategy is :email or :both
      # t.datetime :locked_at


      t.timestamps null: false
    end

    add_index :users, :email,                unique: true
    add_index :users, :reset_password_token, unique: true


    # add_index :users, :confirmation_token,   unique: true
    # add_index :users, :unlock_token,         unique: true
  end
end

/

class RemoveEmailUniquenessFromUser < ActiveRecord::Migration[5.0]
  def change
    change_column :users, :email, :string, unique: false
  end
end

/

class RemoveIndexFromUsersEmail < ActiveRecord::Migration[5.0]

def change
remove_index :users, :email
end

end

编辑:

I have changed devise params to:

    def configure_permitted_parameters
    added_attrs = [:username, :email, :password, :password_confirmation, :remember_me]
    devise_parameter_sanitizer.permit :sign_in, keys: [:username, :password]
    devise_parameter_sanitizer.permit :sign_up, keys: [:username, :password, :password_confirmation]
    devise_parameter_sanitizer.permit :account_update, keys: [:username, :password, :password_confirmation]
  end

仍然,'无效,电子邮件,登录名或密码'

【问题讨论】:

【参考方案1】:

在这里:

def configure_permitted_parameters added_attrs = [:username, :email, :password, :password_confirmation, :remember_me] devise_parameter_sanitizer.permit :sign_up, keys: [:username, :password] devise_parameter_sanitizer.permit :account_update, keys: [:username, :password] end

您没有使用added_attrs 变量。我猜您还需要允许这些字段。

【讨论】:

我应该改变什么?我试图在我编辑的帖子中配置参数并将它们显示在上面。不幸的是,仍然无法正常工作。 如果您进入控制台并运行User.create username: 'whatever', password: 'password',您会得到什么?这样可以保存密码吗?之后您可以使用该用户登录网站吗? 我认为我需要密码确认是错误的,这是需要密码的较旧版本的设计。

以上是关于设计注册成功,登录失败的主要内容,如果未能解决你的问题,请参考以下文章

登陆界面代码

01 | 你真的懂测试吗?从“用户登录”测试谈起 茹炳晟

基于tcp协议的登录,文件上传和下载

用例设计面试题

H5用户登录测试用例

登录功能的测试用例设计