# frozen_string_literal: true module Auth class PasswordsController < Auth::BaseController prepend_before_action :require_no_authentication # Render the #edit only if coming from a reset password email link append_before_action :assert_reset_token_passed, only: :edit # GET /auth/password/new def new self.resource = Auth::PasswordNewForm.new end # POST /resource/password def create self.resource = Auth::PasswordNewForm.new(params[resource_name]) # if successfully_sent?(resource) if resource.submit flash[:notification] = 'auth.new_password.succes' respond_with({}, location: after_sending_reset_password_instructions_path_for(resource_name)) else respond_with(resource) end end def send_reset_pass self.resource = Auth::PasswordNewForm.new(params[resource_name]) respond_to do |format| format.json do if resource.submit render json: resource.to_json else render json: { errors: resource.errors }.to_json, status: :unprocessable_entity end end format.all { render_404 } end end # GET /resource/password/edit?reset_password_token=abcdef def edit self.resource = resource_class.new resource.reset_password_token = params[:reset_password_token] end # PUT /resource/password def update self.resource = resource_class.reset_password_by_token(resource_params) if resource&.errors&.empty? resource.unlock_access! if unlockable?(resource) if is_navigational_format? set_flash_message( :notice, resource.active_for_authentication? ? :updated : :updated_not_active ) end sign_in(resource_name, resource) respond_with resource, location: after_resetting_password_path_for(resource) else # resource.reset_password_token = resource_params[:reset_password_token] respond_with resource end end # GET /resource/password/sent def sent @structure = ContentStorageType.password_sent.storage end protected def after_resetting_password_path_for(resource) after_sign_in_path_for(resource) end # The path used after sending reset password instructions def after_sending_reset_password_instructions_path_for(_resource_name) # new_auth_session_path if is_navigational_format? sent_auth_password_path if is_navigational_format? end # Check if a reset_password_token is provided in the request def assert_reset_token_passed if params[:reset_password_token].blank? set_flash_message(:alert, :no_token) redirect_to new_session_path(resource_name) end end # Check if proper Lockable module methods are present & unlock strategy # allows to unlock resource on password reset def unlockable?(resource) resource.respond_to?(:unlock_access!) && resource.respond_to?(:unlock_strategy_enabled?) && resource.unlock_strategy_enabled?(:email) end end end