class PpaParticipativo::Cidadaos::SessionsController < Devise::SessionsController
	before_action :verifica_liberacao_do_ppa_participativo, only: [:new, :create]

	# GET /resource/sign_in
	def new
		self.resource = resource_class.new(sign_in_params)
		clean_up_passwords(resource)
		yield resource if block_given?
		respond_with(resource, serialize_options(resource))
	end

	# POST /resource/sign_in
	def create
		self.resource = warden.authenticate!(auth_options)
		set_flash_message!(:notice, :signed_in)
		sign_in(resource_name, resource)
		yield resource if block_given?
		respond_with resource, location: after_sign_in_path_for(resource)
	end

	def after_sign_in_path_for(resource)
		stored_location_for(:cidadao) || ppa_participativo_participe_path
	end

	def after_sign_out_path_for(resource_or_scope)
		ppa_participativo_path
	end

	private
	def verifica_liberacao_do_ppa_participativo
		@ppa_atual = Ppa::Ppa.find_by(ppa_participativo_ativo: true)
		redirect_to ppa_participativo_path unless @ppa_atual
	end
end
