diff --git a/app/assets/javascripts/discourse/app/controllers/preferences/second-factor.js b/app/assets/javascripts/discourse/app/controllers/preferences/second-factor.js index 4ced1bc0752..81fc760ecfb 100644 --- a/app/assets/javascripts/discourse/app/controllers/preferences/second-factor.js +++ b/app/assets/javascripts/discourse/app/controllers/preferences/second-factor.js @@ -21,17 +21,12 @@ export default Controller.extend(CanCheckEmails, { modal: service(), loading: false, dirty: false, - resetPasswordLoading: false, - resetPasswordProgress: "", - password: null, errorMessage: null, newUsername: null, backupEnabled: alias("model.second_factor_backup_enabled"), secondFactorMethod: SECOND_FACTOR_METHODS.TOTP, totps: null, - loaded: false, - init() { this._super(...arguments); this.set("totps", []); @@ -47,6 +42,11 @@ export default Controller.extend(CanCheckEmails, { return user && user.enforcedSecondFactor; }, + @discourseComputed("totps", "security_keys") + hasSecondFactors(totps, security_keys) { + return totps.length > 0 || security_keys.length > 0; + }, + @action handleError(error) { if (error.jqXHR) { @@ -81,7 +81,7 @@ export default Controller.extend(CanCheckEmails, { this.set("loading", true); this.model - .loadSecondFactorCodes(this.password) + .loadSecondFactorCodes() .then((response) => { if (response.error) { this.set("errorMessage", response.error); @@ -90,17 +90,10 @@ export default Controller.extend(CanCheckEmails, { this.setProperties({ errorMessage: null, - loaded: true, totps: response.totps, security_keys: response.security_keys, - password: null, dirty: false, }); - this.set( - "model.second_factor_enabled", - (response.totps && response.totps.length > 0) || - (response.security_keys && response.security_keys.length > 0) - ); }) .catch((e) => this.handleError(e)) .finally(() => this.set("loading", false)); @@ -111,37 +104,7 @@ export default Controller.extend(CanCheckEmails, { this.set("dirty", true); }, - @action - resetPassword(event) { - event?.preventDefault(); - - this.setProperties({ - resetPasswordLoading: true, - resetPasswordProgress: "", - }); - - return this.model - .changePassword() - .then(() => { - this.set( - "resetPasswordProgress", - I18n.t("user.change_password.success") - ); - }) - .catch(popupAjaxError) - .finally(() => this.set("resetPasswordLoading", false)); - }, - actions: { - confirmPassword() { - if (!this.password) { - return; - } - this.markDirty(); - this.loadSecondFactors(); - this.set("password", null); - }, - disableAllSecondFactors() { if (this.loading) { return; diff --git a/app/assets/javascripts/discourse/app/controllers/preferences/security.js b/app/assets/javascripts/discourse/app/controllers/preferences/security.js index 3f3ce94cbd6..9483dd87a21 100644 --- a/app/assets/javascripts/discourse/app/controllers/preferences/security.js +++ b/app/assets/javascripts/discourse/app/controllers/preferences/security.js @@ -2,6 +2,7 @@ import Controller from "@ember/controller"; import { action } from "@ember/object"; import { gt } from "@ember/object/computed"; import { inject as service } from "@ember/service"; +import ConfirmSession from "discourse/components/dialog-messages/confirm-session"; import AuthTokenModal from "discourse/components/modal/auth-token"; import { ajax } from "discourse/lib/ajax"; import { popupAjaxError } from "discourse/lib/ajax-error"; @@ -17,6 +18,8 @@ const DEFAULT_AUTH_TOKENS_COUNT = 2; export default Controller.extend(CanCheckEmails, { modal: service(), + dialog: service(), + router: service(), passwordProgress: null, subpageTitle: I18n.t("user.preferences_nav.security"), showAllAuthTokens: false, @@ -114,6 +117,27 @@ export default Controller.extend(CanCheckEmails, { .catch(popupAjaxError); }, + @action + async manage2FA() { + try { + const trustedSession = await this.model.trustedSession(); + + if (!trustedSession.success) { + this.dialog.dialog({ + title: I18n.t("user.confirm_access.title"), + type: "notice", + bodyComponent: ConfirmSession, + didConfirm: () => + this.router.transitionTo("preferences.second-factor"), + }); + } else { + await this.router.transitionTo("preferences.second-factor"); + } + } catch (error) { + popupAjaxError(error); + } + }, + actions: { save() { this.set("saved", false); diff --git a/app/assets/javascripts/discourse/app/models/user.js b/app/assets/javascripts/discourse/app/models/user.js index f050d3905af..aea705bb47a 100644 --- a/app/assets/javascripts/discourse/app/models/user.js +++ b/app/assets/javascripts/discourse/app/models/user.js @@ -540,9 +540,8 @@ const User = RestModel.extend({ }); }, - loadSecondFactorCodes(password) { + loadSecondFactorCodes() { return ajax("/u/second_factors.json", { - data: { password }, type: "POST", }); }, diff --git a/app/assets/javascripts/discourse/app/routes/preferences-second-factor.js b/app/assets/javascripts/discourse/app/routes/preferences-second-factor.js index 189741aa1d6..514639b64e2 100644 --- a/app/assets/javascripts/discourse/app/routes/preferences-second-factor.js +++ b/app/assets/javascripts/discourse/app/routes/preferences-second-factor.js @@ -5,6 +5,7 @@ import RestrictedUserRoute from "discourse/routes/restricted-user"; export default RestrictedUserRoute.extend({ currentUser: service(), siteSettings: service(), + router: service(), model() { return this.modelFor("user"); @@ -15,15 +16,15 @@ export default RestrictedUserRoute.extend({ controller.set("loading", true); model - .loadSecondFactorCodes("") + .loadSecondFactorCodes() .then((response) => { if (response.error) { controller.set("errorMessage", response.error); + } else if (response.unconfirmed_session) { + this.router.transitionTo("preferences.security"); } else { controller.setProperties({ errorMessage: null, - loaded: !response.password_required, - dirty: !!response.password_required, totps: response.totps, security_keys: response.security_keys, }); diff --git a/app/assets/javascripts/discourse/app/templates/preferences-second-factor.hbs b/app/assets/javascripts/discourse/app/templates/preferences-second-factor.hbs index 48c946884f1..08e69013270 100644 --- a/app/assets/javascripts/discourse/app/templates/preferences-second-factor.hbs +++ b/app/assets/javascripts/discourse/app/templates/preferences-second-factor.hbs @@ -19,194 +19,140 @@