A platform for community discussion. Free, open, simple.
DEV: Capture destination url and redirect when required user fields are present (#40207)
Chris Alberti93df16f0a624
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rbindex 57dfd0164ff..00f98ee6845 100644--- a/app/controllers/application_controller.rb+++ b/app/controllers/application_controller.rb@@ -880,6 +880,8 @@ class ApplicationController < ActionController::Basesecond_factor_path = path("/u/#{current_user.encoded_username}/preferences/second-factor")allowed_paths = [redirect_path, second_factor_path, path("/admin"), path("/safe-mode")]if allowed_paths.none? { |p| request.fullpath.start_with?(p) }+ cookies[:destination_url] = destination_url.presence+rate_limiter = RateLimiter.new(current_user, "redirect_to_required_fields_log", 1, 24.hours)if rate_limiter.performed!(raise_error: false)diff --git a/frontend/discourse/app/controllers/preferences/profile.js b/frontend/discourse/app/controllers/preferences/profile.jsindex 2861c11efa4..f89882c57af 100644--- a/frontend/discourse/app/controllers/preferences/profile.js+++ b/frontend/discourse/app/controllers/preferences/profile.js@@ -5,6 +5,7 @@ import { compare, isEmpty } from "@ember/utils";import FeatureTopicOnProfileModal from "discourse/components/modal/feature-topic-on-profile";import { ajax } from "discourse/lib/ajax";import { popupAjaxError } from "discourse/lib/ajax-error";+import cookie, { removeCookie } from "discourse/lib/cookie";import { applyValueTransformer } from "discourse/lib/transformer";import { i18n } from "discourse-i18n";@@ -183,6 +184,12 @@ export default class ProfileController extends Controller {this.model.set("bio_cooked", user.bio_cooked);this.currentUser.set("needs_required_fields_check", false);this.set("saved", true);++ const destinationUrl = cookie("destination_url");+ if (destinationUrl) {+ removeCookie("destination_url", { path: "/" });+ window.location.href = destinationUrl;+ }}).catch(popupAjaxError);}diff --git a/spec/system/user_page/user_preferences_profile_spec.rb b/spec/system/user_page/user_preferences_profile_spec.rbindex 322b8afa01b..d2b57af9d01 100644--- a/spec/system/user_page/user_preferences_profile_spec.rb+++ b/spec/system/user_page/user_preferences_profile_spec.rb@@ -126,5 +126,18 @@ describe "User preferences | Profile" doexpect(page).to have_current_path("/u/#{user.username}/preferences/profile")end++ it "redirects back to the original destination after filling required fields" do+ category = Fabricate(:category)+ visit("/new-topic?category_id=#{category.id}")++ expect(page).to have_current_path("/u/#{user.username}/preferences/profile")++ find(".user-field-favourite-pokemon input").fill_in(with: "Mudkip")+ find(".user-field-updated-terms input").check+ find(".save-button .btn-primary").click++ expect(page).to have_current_path("/new-topic?category_id=#{category.id}")+ endendend