Skip to content

Commit ef29f56

Browse files
authored
Merge pull request #278 from stevenharman/remove_cookie_on_signout_by_specifying_domain
Remove cookie on signout by specifying domain
2 parents eee3e75 + d8dd37b commit ef29f56

File tree

3 files changed

+40
-19
lines changed

3 files changed

+40
-19
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ class VisitorsController < ApplicationController
116116

117117
protected
118118
def intercom_shutdown
119-
IntercomRails::ShutdownHelper.intercom_shutdown(session, cookies)
119+
IntercomRails::ShutdownHelper.intercom_shutdown(session, cookies, request.domain)
120120
end
121121
end
122122
```
@@ -126,7 +126,7 @@ end
126126
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.
127127

128128
```ruby
129-
IntercomRails::ShutdownHelper::intercom_shutdown_helper(cookies)
129+
IntercomRails::ShutdownHelper::intercom_shutdown_helper(cookies, domain)
130130
```
131131

132132
**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.

lib/intercom-rails/shutdown_helper.rb

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@ module ShutdownHelper
33
# This helper allows to erase cookies when a user log out of an application
44
# It is recommanded to call this function every time a user log out of your application
55
# Do not use before a redirect_to because it will not clear the cookies on a redirection
6-
def self.intercom_shutdown_helper(cookies)
6+
def self.intercom_shutdown_helper(cookies, domain = nil)
7+
nil_session = { value: nil, expires: 1.day.ago }
8+
nil_session = nil_session.merge(domain: domain) unless domain.nil? || domain == 'localhost'
9+
710
if (cookies.is_a?(ActionDispatch::Cookies::CookieJar))
8-
cookies["intercom-session-#{IntercomRails.config.app_id}"] = { value: nil, expires: 1.day.ago}
11+
cookies["intercom-session-#{IntercomRails.config.app_id}"] = nil_session
912
else
1013
controller = cookies
1114
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.")
12-
controller.response.delete_cookie("intercom-session-#{IntercomRails.config.app_id}", { value: nil, expires: 1.day.ago})
15+
controller.response.delete_cookie("intercom-session-#{IntercomRails.config.app_id}", nil_session)
1316
end
1417
rescue
1518
end
@@ -18,10 +21,10 @@ def self.prepare_intercom_shutdown(session)
1821
session[:perform_intercom_shutdown] = true
1922
end
2023

21-
def self.intercom_shutdown(session, cookies)
24+
def self.intercom_shutdown(session, cookies, domain = nil)
2225
if session[:perform_intercom_shutdown]
2326
session.delete(:perform_intercom_shutdown)
24-
intercom_shutdown_helper(cookies)
27+
intercom_shutdown_helper(cookies, domain)
2528
end
2629
end
2730

spec/shutdown_helper_spec.rb

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,36 @@
33

44
describe TestController, type: :controller do
55
include IntercomRails::ShutdownHelper
6-
it 'clears response intercom-session-{app_id} cookie' do
7-
IntercomRails::ShutdownHelper.intercom_shutdown_helper(cookies)
8-
expect(cookies.has_key?('intercom-session-abc123')).to eq true
6+
context 'without domain' do
7+
it 'clears response intercom-session-{app_id} cookie' do
8+
IntercomRails::ShutdownHelper.intercom_shutdown_helper(cookies)
9+
expect(cookies.has_key?('intercom-session-abc123')).to eq true
10+
end
11+
it 'creates session[:perform_intercom_shutdown] var' do
12+
IntercomRails::ShutdownHelper.prepare_intercom_shutdown(session)
13+
expect(session[:perform_intercom_shutdown]).to eq true
14+
end
15+
it 'erase intercom cookie, set preform_intercom_shutdown sessions to nil' do
16+
session[:perform_intercom_shutdown] = true
17+
IntercomRails::ShutdownHelper.intercom_shutdown(session, cookies)
18+
expect(session[:perform_intercom_shutdown]).to eq nil
19+
expect(cookies.has_key?('intercom-session-abc123')).to eq true
20+
end
921
end
10-
it 'creates session[:perform_intercom_shutdown] var' do
11-
IntercomRails::ShutdownHelper.prepare_intercom_shutdown(session)
12-
expect(session[:perform_intercom_shutdown]).to eq true
13-
end
14-
it 'erase intercom cookie, set preform_intercom_shutdown sessions to nil' do
15-
session[:perform_intercom_shutdown] = true
16-
IntercomRails::ShutdownHelper.intercom_shutdown(session, cookies)
17-
expect(session[:perform_intercom_shutdown]).to eq nil
18-
expect(cookies.has_key?('intercom-session-abc123')).to eq true
22+
context 'with domain' do
23+
it 'clears response intercom-session-{app_id} cookie' do
24+
IntercomRails::ShutdownHelper.intercom_shutdown_helper(cookies, 'intercom.com')
25+
expect(cookies.has_key?('intercom-session-abc123')).to eq true
26+
end
27+
it 'creates session[:perform_intercom_shutdown] var' do
28+
IntercomRails::ShutdownHelper.prepare_intercom_shutdown(session)
29+
expect(session[:perform_intercom_shutdown]).to eq true
30+
end
31+
it 'erase intercom cookie, set preform_intercom_shutdown sessions to nil' do
32+
session[:perform_intercom_shutdown] = true
33+
IntercomRails::ShutdownHelper.intercom_shutdown(session, cookies, 'intercom.com')
34+
expect(session[:perform_intercom_shutdown]).to eq nil
35+
expect(cookies.has_key?('intercom-session-abc123')).to eq true
36+
end
1937
end
2038
end

0 commit comments

Comments
 (0)