Skip to content

Remove cookie on signout by specifying domain #278

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand All @@ -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.
Expand Down
13 changes: 8 additions & 5 deletions lib/intercom-rails/shutdown_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ 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)
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}
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})
controller.response.delete_cookie("intercom-session-#{IntercomRails.config.app_id}", nil_session)
end
rescue
end
Expand All @@ -18,10 +21,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

Expand Down
42 changes: 30 additions & 12 deletions spec/shutdown_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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