A platform for community discussion. Free, open, simple.
FIX: Cleanup authentication_data cookie after login (#11834)
David Taylor2092152b035a
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rbindex 0b994496dc0..0f948222982 100644--- a/app/helpers/application_helper.rb+++ b/app/helpers/application_helper.rb@@ -560,4 +560,16 @@ module ApplicationHelperendendend++ def authentication_data+ return @authentication_data if defined?(@authentication_data)++ @authentication_data = begin+ value = cookies[:authentication_data]+ if value+ cookies.delete(:authentication_data, path: Discourse.base_path("/"))+ end+ current_user ? nil : value+ end+ endenddiff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erbindex 165ece79e1e..796a7be265d 100644--- a/app/views/layouts/application.html.erb+++ b/app/views/layouts/application.html.erb@@ -60,8 +60,8 @@<%= tag.meta id: 'data-discourse-setup', data: client_side_setup_data %>- <%- if !current_user && (data = cookies.delete(:authentication_data, path: Discourse.base_path("/"))) %>- <meta id="data-authentication" data-authentication-data="<%= data %>">+ <%- if authentication_data %>+ <meta id="data-authentication" data-authentication-data="<%= authentication_data %>"><%- end %></head>diff --git a/spec/requests/application_controller_spec.rb b/spec/requests/application_controller_spec.rbindex 5e82551fc2b..1ac69118a79 100644--- a/spec/requests/application_controller_spec.rb+++ b/spec/requests/application_controller_spec.rb@@ -104,11 +104,21 @@ RSpec.describe ApplicationController doendit 'contains authentication data when cookies exist' do- COOKIE_DATA = "someauthenticationdata"- cookies['authentication_data'] = COOKIE_DATA+ cookie_data = "someauthenticationdata"+ cookies['authentication_data'] = cookie_dataget '/login'expect(response.status).to eq(200)- expect(response.body).to include("data-authentication-data=\"#{COOKIE_DATA }\"")+ expect(response.body).to include("data-authentication-data=\"#{cookie_data}\"")+ expect(response.headers["Set-Cookie"]).to include("authentication_data=;") # Delete cookie+ end++ it 'deletes authentication data cookie even if already authenticated' do+ sign_in(Fabricate(:user))+ cookies['authentication_data'] = "someauthenticationdata"+ get '/'+ expect(response.status).to eq(200)+ expect(response.body).not_to include("data-authentication-data=")+ expect(response.headers["Set-Cookie"]).to include("authentication_data=;") # Delete cookieendend