FIX: Keep user in same context after login (#31314)

This fixes the destination of the auth process in the following
scenarios:

- when landing on a PM or a topic as an anonymous user and then loggin
in
- when landing on a public topic, hitting Reply or Like and then logging
in
This commit is contained in:
Penar Musaraj 2025-02-13 10:35:00 -05:00 committed by GitHub
parent 8d709aeb9c
commit 65d7ea2dbc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 75 additions and 4 deletions

View File

@ -137,6 +137,8 @@ export default class Login extends Component {
} else if (destinationUrl) { } else if (destinationUrl) {
removeCookie("destination_url"); removeCookie("destination_url");
window.location.assign(destinationUrl); window.location.assign(destinationUrl);
} else if (this.args.model.referrerUrl) {
window.location.assign(this.args.model.referrerUrl);
} else { } else {
window.location.reload(); window.location.reload();
} }
@ -288,6 +290,8 @@ export default class Login extends Component {
removeCookie("destination_url"); removeCookie("destination_url");
applyHiddenFormInputValue(destinationUrl, "redirect"); applyHiddenFormInputValue(destinationUrl, "redirect");
} else if (this.args.model.referrerUrl) {
applyHiddenFormInputValue(this.args.model.referrerUrl, "redirect");
} else { } else {
applyHiddenFormInputValue(window.location.href, "redirect"); applyHiddenFormInputValue(window.location.href, "redirect");
} }

View File

@ -160,6 +160,8 @@ export default class LoginPageController extends Controller {
} else if (destinationUrl) { } else if (destinationUrl) {
removeCookie("destination_url"); removeCookie("destination_url");
window.location.assign(destinationUrl); window.location.assign(destinationUrl);
} else if (this.referrerUrl) {
window.location.assign(this.referrerUrl);
} else { } else {
window.location.reload(); window.location.reload();
} }
@ -337,6 +339,8 @@ export default class LoginPageController extends Controller {
removeCookie("destination_url"); removeCookie("destination_url");
applyHiddenFormInputValue(destinationUrl, "redirect"); applyHiddenFormInputValue(destinationUrl, "redirect");
} else if (this.referrerUrl) {
applyHiddenFormInputValue(this.referrerUrl, "redirect");
} else { } else {
applyHiddenFormInputValue(window.location.href, "redirect"); applyHiddenFormInputValue(window.location.href, "redirect");
} }

View File

@ -298,6 +298,9 @@ export default class ApplicationRoute extends DiscourseRoute {
showNotActivated: (props) => this.send("showNotActivated", props), showNotActivated: (props) => this.send("showNotActivated", props),
showCreateAccount: (props) => this.send("showCreateAccount", props), showCreateAccount: (props) => this.send("showCreateAccount", props),
canSignUp: this.controller.canSignUp, canSignUp: this.controller.canSignUp,
referrerUrl: DiscourseURL.isInternal(document.referrer)
? document.referrer
: null,
}, },
}); });
} }

View File

@ -1,5 +1,6 @@
import { next } from "@ember/runloop"; import { next } from "@ember/runloop";
import { service } from "@ember/service"; import { service } from "@ember/service";
import DiscourseURL from "discourse/lib/url";
import { defaultHomepage } from "discourse/lib/utilities"; import { defaultHomepage } from "discourse/lib/utilities";
import StaticPage from "discourse/models/static-page"; import StaticPage from "discourse/models/static-page";
import DiscourseRoute from "discourse/routes/discourse"; import DiscourseRoute from "discourse/routes/discourse";
@ -9,7 +10,11 @@ export default class LoginRoute extends DiscourseRoute {
@service router; @service router;
@service login; @service login;
beforeModel() { beforeModel(transition) {
if (transition.from) {
this.internalReferrer = this.router.urlFor(transition.from.name);
}
if (this.siteSettings.login_required) { if (this.siteSettings.login_required) {
if ( if (
this.login.isOnlyOneExternalLoginMethod && this.login.isOnlyOneExternalLoginMethod &&
@ -49,6 +54,10 @@ export default class LoginRoute extends DiscourseRoute {
controller.set("flashType", ""); controller.set("flashType", "");
controller.set("flash", ""); controller.set("flash", "");
if (this.internalReferrer || DiscourseURL.isInternal(document.referrer)) {
controller.set("referrerUrl", this.internalReferrer || document.referrer);
}
if (this.siteSettings.login_required) { if (this.siteSettings.login_required) {
controller.set("showLogin", false); controller.set("showLogin", false);
} }

View File

@ -88,9 +88,6 @@ shared_examples "login scenarios" do |login_page_object|
# TODO: prefill username when fullpage # TODO: prefill username when fullpage
if find("#username-or-email").value.blank? if find("#username-or-email").value.blank?
if page.has_css?("html.mobile-view", wait: 0)
expect(page).to have_no_css(".d-modal.is-animating")
end
find("#username-or-email").fill_in(with: user.username) find("#username-or-email").fill_in(with: user.username)
end end
@ -126,6 +123,60 @@ shared_examples "login scenarios" do |login_page_object|
login_form.fill(username: "john", password: "supersecurepassword").click_login login_form.fill(username: "john", password: "supersecurepassword").click_login
expect(page).to have_css(".header-dropdown-toggle.current-user") expect(page).to have_css(".header-dropdown-toggle.current-user")
end end
it "redirects to a PM after login" do
EmailToken.confirm(Fabricate(:email_token, user: user).token)
group = Fabricate(:group, publish_read_state: true)
Fabricate(:group_user, group: group, user: user)
pm = Fabricate(:private_message_topic, allowed_groups: [group])
Fabricate(:post, topic: pm, user: user, reads: 2, created_at: 1.day.ago)
Fabricate(:group_private_message_topic, user: user, recipient_group: group)
visit "/t/#{pm.id}"
find(".login-welcome .login-button").click
login_form.fill(username: "john", password: "supersecurepassword").click_login
expect(page).to have_css(".header-dropdown-toggle.current-user")
expect(page).to have_css("#topic-title")
expect(page).to have_css(".private_message")
end
end
context "when login is not required" do
before { SiteSetting.login_required = false }
it "redirects to a PM after authentication" do
EmailToken.confirm(Fabricate(:email_token, user: user).token)
group = Fabricate(:group, publish_read_state: true)
Fabricate(:group_user, group: group, user: user)
pm = Fabricate(:private_message_topic, allowed_groups: [group])
Fabricate(:post, topic: pm, user: user, reads: 2, created_at: 1.day.ago)
Fabricate(:group_private_message_topic, user: user, recipient_group: group)
visit "/t/#{pm.id}"
find(".btn.login-button").click
login_form.fill(username: "john", password: "supersecurepassword").click_login
expect(page).to have_css(".header-dropdown-toggle.current-user")
expect(page).to have_css("#topic-title")
expect(page).to have_css(".private_message")
end
it "redirects to a public topic when hitting Reply then logging in" do
EmailToken.confirm(Fabricate(:email_token, user: user).token)
topic = Fabricate(:topic)
Fabricate(:post, topic: topic, created_at: 1.day.ago)
visit "/t/#{topic.id}"
find(".topic-footer-main-buttons .btn-primary").click
login_form.fill(username: "john", password: "supersecurepassword").click_login
expect(page).to have_css(".header-dropdown-toggle.current-user")
expect(page).to have_css("#topic-title")
end
end end
context "with two-factor authentication" do context "with two-factor authentication" do