diff --git a/app/assets/javascripts/discourse/app/routes/application.js b/app/assets/javascripts/discourse/app/routes/application.js index 5a12117a151..9d0b55e592a 100644 --- a/app/assets/javascripts/discourse/app/routes/application.js +++ b/app/assets/javascripts/discourse/app/routes/application.js @@ -76,6 +76,11 @@ const ApplicationRoute = DiscourseRoute.extend({ !this.restrictedRouting.isAllowedRoute(transition.to.name) ) { transition.abort(); + this.router.replaceWith( + this.restrictedRouting.redirectRoute, + this.currentUser + ); + return false; } diff --git a/app/assets/javascripts/discourse/app/services/restricted-routing.js b/app/assets/javascripts/discourse/app/services/restricted-routing.js index 998c988374a..525a65be729 100644 --- a/app/assets/javascripts/discourse/app/services/restricted-routing.js +++ b/app/assets/javascripts/discourse/app/services/restricted-routing.js @@ -38,6 +38,16 @@ export default class RestrictedRouting extends Service { return true; } + get redirectRoute() { + if (this._needs2fa) { + return "preferences.second-factor"; + } + + if (this._needsRequiredFields) { + return "preferences.profile"; + } + } + get _needs2fa() { // NOTE: Matches the should_enforce_2fa? and disqualified_from_2fa_enforcement // methods in ApplicationController. diff --git a/spec/system/user_page/user_preferences_profile_spec.rb b/spec/system/user_page/user_preferences_profile_spec.rb index 66ec74364d9..f31ee17ced4 100644 --- a/spec/system/user_page/user_preferences_profile_spec.rb +++ b/spec/system/user_page/user_preferences_profile_spec.rb @@ -31,7 +31,7 @@ describe "User preferences | Profile", type: :system do ) end - it "redirects to the profile page to fill up required fields" do + it "server-side redirects to the profile page to fill up required fields" do visit("/") expect(page).to have_current_path("/u/#{user.username}/preferences/profile") @@ -42,6 +42,21 @@ describe "User preferences | Profile", type: :system do ) end + it "client-side redirects to the profile page to fill up required fields" do + visit("/faq") + + expect(page).to have_current_path("/faq") + + find("#site-logo").click + + expect(page).to have_current_path("/u/#{user.username}/preferences/profile") + + expect(page).to have_selector( + ".alert-error", + text: I18n.t("js.user.preferences.profile.enforced_required_fields"), + ) + end + it "disables client-side routing while missing required fields" do user_preferences_profile_page.visit(user)