From bb06d8337a202ccf09a9f0eaa79008d0de4ea050 Mon Sep 17 00:00:00 2001 From: Bastien Libersa Date: Thu, 20 Jul 2017 16:19:53 +0200 Subject: [PATCH 1/2] Fixes GitHub issue #194 --- README.md | 4 +-- lib/intercom-rails/shutdown_helper.rb | 10 +++---- spec/shutdown_helper_spec.rb | 42 +++++++++++++++++++-------- 3 files changed, 37 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index c59f322..2570b8f 100644 --- a/README.md +++ b/README.md @@ -115,7 +115,7 @@ class VisitorsController < ApplicationController protected def intercom_shutdown - IntercomRails::ShutdownHelper.intercom_shutdown(session, cookies) + IntercomRails::ShutdownHelper.intercom_shutdown(session, cookies, request.domain) end end ``` @@ -125,7 +125,7 @@ end If you use another service than Devise or if you implemented your own authentication service, you can call the following method in a controller to shutdown Intercom on logout. ```ruby -IntercomRails::ShutdownHelper::intercom_shutdown_helper(cookies) +IntercomRails::ShutdownHelper::intercom_shutdown_helper(cookies, domain) ``` **Be aware that if you call this method before a 'redirect_to' (quite common on logout) it will have no impact** as it is impossible to update cookies when you use a redirection. diff --git a/lib/intercom-rails/shutdown_helper.rb b/lib/intercom-rails/shutdown_helper.rb index 3524fc3..398b9bf 100644 --- a/lib/intercom-rails/shutdown_helper.rb +++ b/lib/intercom-rails/shutdown_helper.rb @@ -3,13 +3,13 @@ module ShutdownHelper # This helper allows to erase cookies when a user log out of an application # It is recommanded to call this function every time a user log out of your application # Do not use before a redirect_to because it will not clear the cookies on a redirection - def self.intercom_shutdown_helper(cookies) + def self.intercom_shutdown_helper(cookies, domain = nil) if (cookies.is_a?(ActionDispatch::Cookies::CookieJar)) - cookies["intercom-session-#{IntercomRails.config.app_id}"] = { value: nil, expires: 1.day.ago} + cookies["intercom-session-#{IntercomRails.config.app_id}"] = { value: nil, expires: 1.day.ago }.merge(domain.present? ? { domain: ".#{domain}"} : {}) else controller = cookies Rails.logger.info("Warning: IntercomRails::ShutdownHelper.intercom_shutdown_helper takes an instance of ActionDispatch::Cookies::CookieJar as an argument since v0.2.34. Passing a controller is depreciated. See https://github.com/intercom/intercom-rails#shutdown for more details.") - controller.response.delete_cookie("intercom-session-#{IntercomRails.config.app_id}", { value: nil, expires: 1.day.ago}) + controller.response.delete_cookie("intercom-session-#{IntercomRails.config.app_id}", { value: nil, expires: 1.day.ago }).merge(domain.present? ? { domain: ".#{domain}"} : {}) end rescue end @@ -18,10 +18,10 @@ def self.prepare_intercom_shutdown(session) session[:perform_intercom_shutdown] = true end - def self.intercom_shutdown(session, cookies) + def self.intercom_shutdown(session, cookies, domain = nil) if session[:perform_intercom_shutdown] session.delete(:perform_intercom_shutdown) - intercom_shutdown_helper(cookies) + intercom_shutdown_helper(cookies, domain) end end diff --git a/spec/shutdown_helper_spec.rb b/spec/shutdown_helper_spec.rb index fb44096..40a1301 100644 --- a/spec/shutdown_helper_spec.rb +++ b/spec/shutdown_helper_spec.rb @@ -3,18 +3,36 @@ describe TestController, type: :controller do include IntercomRails::ShutdownHelper - it 'clears response intercom-session-{app_id} cookie' do - IntercomRails::ShutdownHelper.intercom_shutdown_helper(cookies) - expect(cookies.has_key?('intercom-session-abc123')).to eq true + context 'without domain' do + it 'clears response intercom-session-{app_id} cookie' do + IntercomRails::ShutdownHelper.intercom_shutdown_helper(cookies) + expect(cookies.has_key?('intercom-session-abc123')).to eq true + end + it 'creates session[:perform_intercom_shutdown] var' do + IntercomRails::ShutdownHelper.prepare_intercom_shutdown(session) + expect(session[:perform_intercom_shutdown]).to eq true + end + it 'erase intercom cookie, set preform_intercom_shutdown sessions to nil' do + session[:perform_intercom_shutdown] = true + IntercomRails::ShutdownHelper.intercom_shutdown(session, cookies) + expect(session[:perform_intercom_shutdown]).to eq nil + expect(cookies.has_key?('intercom-session-abc123')).to eq true + end end - it 'creates session[:perform_intercom_shutdown] var' do - IntercomRails::ShutdownHelper.prepare_intercom_shutdown(session) - expect(session[:perform_intercom_shutdown]).to eq true - end - it 'erase intercom cookie, set preform_intercom_shutdown sessions to nil' do - session[:perform_intercom_shutdown] = true - IntercomRails::ShutdownHelper.intercom_shutdown(session, cookies) - expect(session[:perform_intercom_shutdown]).to eq nil - expect(cookies.has_key?('intercom-session-abc123')).to eq true + context 'with domain' do + it 'clears response intercom-session-{app_id} cookie' do + IntercomRails::ShutdownHelper.intercom_shutdown_helper(cookies, 'intercom.com') + expect(cookies.has_key?('intercom-session-abc123')).to eq true + end + it 'creates session[:perform_intercom_shutdown] var' do + IntercomRails::ShutdownHelper.prepare_intercom_shutdown(session) + expect(session[:perform_intercom_shutdown]).to eq true + end + it 'erase intercom cookie, set preform_intercom_shutdown sessions to nil' do + session[:perform_intercom_shutdown] = true + IntercomRails::ShutdownHelper.intercom_shutdown(session, cookies, 'intercom.com') + expect(session[:perform_intercom_shutdown]).to eq nil + expect(cookies.has_key?('intercom-session-abc123')).to eq true + end end end From d8dd37b9227765b89bee18e352cdefc40e08b145 Mon Sep 17 00:00:00 2001 From: Steven Harman Date: Wed, 28 Feb 2018 21:01:21 -0500 Subject: [PATCH 2/2] Include domain when present & not nil Adding the `domain` key to the cookie when it's either not present, or `localhost` results in the cookie not being deleted. The code already handled the nil case, but not the `localhost` case. This handles both. --- lib/intercom-rails/shutdown_helper.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/intercom-rails/shutdown_helper.rb b/lib/intercom-rails/shutdown_helper.rb index 398b9bf..8256790 100644 --- a/lib/intercom-rails/shutdown_helper.rb +++ b/lib/intercom-rails/shutdown_helper.rb @@ -4,12 +4,15 @@ module ShutdownHelper # It is recommanded to call this function every time a user log out of your application # Do not use before a redirect_to because it will not clear the cookies on a redirection def self.intercom_shutdown_helper(cookies, domain = nil) + nil_session = { value: nil, expires: 1.day.ago } + nil_session = nil_session.merge(domain: domain) unless domain.nil? || domain == 'localhost' + if (cookies.is_a?(ActionDispatch::Cookies::CookieJar)) - cookies["intercom-session-#{IntercomRails.config.app_id}"] = { value: nil, expires: 1.day.ago }.merge(domain.present? ? { domain: ".#{domain}"} : {}) + cookies["intercom-session-#{IntercomRails.config.app_id}"] = nil_session else controller = cookies Rails.logger.info("Warning: IntercomRails::ShutdownHelper.intercom_shutdown_helper takes an instance of ActionDispatch::Cookies::CookieJar as an argument since v0.2.34. Passing a controller is depreciated. See https://github.com/intercom/intercom-rails#shutdown for more details.") - controller.response.delete_cookie("intercom-session-#{IntercomRails.config.app_id}", { value: nil, expires: 1.day.ago }).merge(domain.present? ? { domain: ".#{domain}"} : {}) + controller.response.delete_cookie("intercom-session-#{IntercomRails.config.app_id}", nil_session) end rescue end