Rails + Devise: принудительный сброс пароля при первом входе - PullRequest
0 голосов
/ 11 марта 2020

(ПРИМЕЧАНИЕ: я уже искал ({ ссылка }), и есть несколько похожих вопросов, но ни один из ответов не кажется мне подходящим.)

Как вы заставить пользователя сбросить свой пароль при первом входе в систему, используя гем devise? Я хотел потребовать, чтобы новые пользователи изменили свой пароль в application_controller.rb:

class ApplicationController < ActionController::Base

  before_action :authenticate_user!
  before_action :configure_permitted_parameters, if: :devise_controller?
  before_action :check_password_reset, only: [:create]

  def after_sign_in_path_for(resource)
    if current_user.needs_password_reset
      password_path(current_user)
    else
      root_path
    end
  end

  protected

  def check_password_reset
    # have also tried:
    # if current_user && current_user.needs_password_reset
    if current_user && current_user.sign_in_count == 1
      flash[:alert] = "You must change your password now."
      redirect_to edit_user_password_path
    end
  end

end

Но при первом входе в систему никому не предлагается изменить свой пароль.

Для дополнительной информации, вот что находится в user.rb:

class User < ApplicationRecord

  devise :rememberable, :trackable, :lockable,
         :database_authenticatable, :timeoutable,
         :recoverable

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

end
...