Skip to content

A platform for community discussion. Free, open, simple.

FEATURE: Preserve the query when redirecting to login/signup (#34363)

Juan David Martínez Cubillos1c4cfa313ea3
diff --git a/app/assets/javascripts/discourse/app/routes/login.js b/app/assets/javascripts/discourse/app/routes/login.js
index a9c8914b044..afd87bcb2ca 100644
--- a/app/assets/javascripts/discourse/app/routes/login.js
+++ b/app/assets/javascripts/discourse/app/routes/login.js
@@ -27,6 +27,7 @@ export default class extends DiscourseRoute {
const { auth_immediately, enable_discourse_connect, login_required } =
this.siteSettings;
const { pathname: url } = window.location;
+ const { search: query } = window.location;
const { referrer } = document;
const { isOnlyOneExternalLoginMethod, singleExternalLogin } = this.login;
const redirect = auth_immediately || login_required || !from || wantsTo;
@@ -51,7 +52,7 @@ export default class extends DiscourseRoute {
// Automatically store the current URL (aka. the one **before** the transition)
if (!currentUser) {
if (isValidDestinationUrl(url)) {
- cookie("destination_url", url);
+ cookie("destination_url", url + query);
} else if (DiscourseURL.isInternalTopic(referrer)) {
cookie("destination_url", referrer);
}
diff --git a/app/assets/javascripts/discourse/app/routes/signup.js b/app/assets/javascripts/discourse/app/routes/signup.js
index ce59f43c43e..073bc017847 100644
--- a/app/assets/javascripts/discourse/app/routes/signup.js
+++ b/app/assets/javascripts/discourse/app/routes/signup.js
@@ -32,6 +32,7 @@ export default class extends DiscourseRoute {
login_required,
} = this.siteSettings;
const { pathname: url } = window.location;
+ const { search: query } = window.location;
const { referrer } = document;
const { canSignUp } = this.controllerFor("application");
const { isOnlyOneExternalLoginMethod, singleExternalLogin } = this.login;
@@ -64,7 +65,7 @@ export default class extends DiscourseRoute {
// Automatically store the current URL (aka. the one **before** the transition)
if (!currentUser) {
if (isValidDestinationUrl(url)) {
- cookie("destination_url", url);
+ cookie("destination_url", url + query);
} else if (DiscourseURL.isInternalTopic(referrer)) {
cookie("destination_url", referrer);
}
diff --git a/spec/requests/static_controller_spec.rb b/spec/requests/static_controller_spec.rb
index f6f9714edc6..93237d9b8b7 100644
--- a/spec/requests/static_controller_spec.rb
+++ b/spec/requests/static_controller_spec.rb
@@ -272,9 +272,16 @@ RSpec.describe StaticController do
expect(response).to redirect_to("/")
end
- it "sets the destination_url cookie when not logged in and path is /login with valid redirect param" do
- get "/login", params: { redirect: "/foo" }
- expect(response.cookies["destination_url"]).to eq("/foo")
+ context "when setting the destination_url cookie" do
+ it "respects the redirect parameter in the cookie" do
+ get "/login", params: { redirect: "/foo" }
+ expect(response.cookies["destination_url"]).to eq("/foo")
+ end
+
+ it "respects the redirect parameter including the query params" do
+ get "/login", params: { redirect: "/foo?filter=test" }
+ expect(response.cookies["destination_url"]).to eq("/foo?filter=test")
+ end
end
end
@@ -299,9 +306,16 @@ RSpec.describe StaticController do
expect(response).to redirect_to("/sub_test/")
end
- it "sets the destination_url cookie when not logged in and path is /login with valid redirect param" do
- get "/login", params: { redirect: "/foo" }
- expect(response.cookies["destination_url"]).to eq("/sub_test/foo")
+ context "when sets the destination_url cookie" do
+ it "respects the redirect parameter in the cookie" do
+ get "/login", params: { redirect: "/foo" }
+ expect(response.cookies["destination_url"]).to eq("/sub_test/foo")
+ end
+
+ it "respects the redirect parameter including the query params" do
+ get "/login", params: { redirect: "/foo?filter=test" }
+ expect(response.cookies["destination_url"]).to eq("/sub_test/foo?filter=test")
+ end
end
end
end
diff --git a/spec/requests/users_controller_spec.rb b/spec/requests/users_controller_spec.rb
index b85573c37b7..c2aabc5d900 100644
--- a/spec/requests/users_controller_spec.rb
+++ b/spec/requests/users_controller_spec.rb
@@ -178,14 +178,27 @@ RSpec.describe UsersController do
end
context "when cookies contains a destination URL" do
- it "should redirect to the URL" do
- destination_url = "http://thisisasite.com/somepath"
- cookies[:destination_url] = destination_url
+ context "when the destination URL has a query" do
+ it "should redirect to the URL and preserve the query" do
+ destination_url = "http://thisisasite.com/somepath?latest=1"
+ cookies[:destination_url] = destination_url
- put "/u/activate-account/#{email_token.token}"
+ put "/u/activate-account/#{email_token.token}"
- expect(response.status).to eq(200)
- expect(response.parsed_body["redirect_to"]).to eq(destination_url)
+ expect(response.status).to eq(200)
+ expect(response.parsed_body["redirect_to"]).to eq(destination_url)
+ end
+ end
+ context "when destination URL doesn't have a query" do
+ it "should redirect to the URL" do
+ destination_url = "http://thisisasite.com/somepath"
+ cookies[:destination_url] = destination_url
+
+ put "/u/activate-account/#{email_token.token}"
+
+ expect(response.status).to eq(200)
+ expect(response.parsed_body["redirect_to"]).to eq(destination_url)
+ end
end
end
diff --git a/spec/system/login_spec.rb b/spec/system/login_spec.rb
index f4b99526e21..ceb5afb3de7 100644
--- a/spec/system/login_spec.rb
+++ b/spec/system/login_spec.rb
@@ -154,6 +154,17 @@ shared_examples "login scenarios" do
expect(page).to have_css(".private_message")
end
+ it "redirects to a route with a query parameters after login and keeps the query parameters" do
+ EmailToken.confirm(Fabricate(:email_token, user: user).token)
+
+ category = Fabricate(:category)
+
+ visit "/new-topic?category_id=#{category.id}"
+ login_form.fill(username: "john", password: "supersecurepassword").click_login
+
+ expect(page).to have_current_path("/new-topic?category_id=#{category.id}")
+ end
+
it "does not leak topics" do
visit "/"
@@ -208,6 +219,17 @@ shared_examples "login scenarios" do
expect(page).to have_css("#topic-title")
end
+ it "redirects to a route with a query parameters after login and keeps the query parameters" do
+ EmailToken.confirm(Fabricate(:email_token, user: user).token)
+
+ category = Fabricate(:category)
+
+ visit "/new-topic?category_id=#{category.id}"
+ login_form.fill(username: "john", password: "supersecurepassword").click_login
+
+ expect(page).to have_current_path("/new-topic?category_id=#{category.id}")
+ end
+
context "with user api key and omniauth" do
include OmniauthHelpers
diff --git a/spec/system/signup_spec.rb b/spec/system/signup_spec.rb
index b839742bc75..71bd94fc549 100644
--- a/spec/system/signup_spec.rb
+++ b/spec/system/signup_spec.rb
@@ -101,6 +101,33 @@ shared_examples "signup scenarios" do
expect(page).to have_current_path("/t/#{topic.slug}/#{topic.id}")
end
+ it "redirects to a new-topic after signin and keeps the query" do
+ category = Fabricate(:category)
+ SiteSetting.default_trust_level = 4
+
+ visit "/new-topic?category_id=#{category.id}"
+ signup_page
+ .open
+ .fill_email("[email protected]")
+ .fill_username("john11122")
+ .fill_password("supersecurepassword")
+
+ expect(signup_page).to have_valid_fields
+
+ signup_page.click_create_account
+
+ expect(page).to have_current_path("/u/account-created")
+
+ mail = ActionMailer::Base.deliveries.first
+ activation_link = mail.body.to_s[%r{/u/activate-account/\S+}]
+
+ visit activation_link
+
+ activate_account.click_activate_account
+
+ expect(page).to have_current_path("/new-topic?category_id=#{category.id}")
+ end
+
it "cannot signup with a common password" do
signup_page.open.fill_email(invite.email).fill_username("john").fill_password("0123456789")
expect(signup_page).to have_valid_fields