REFACTOR: login-controller (#7514)

REFACTOR: login-controller (#7514)

diff --git a/app/assets/javascripts/discourse/controllers/login.js.es6 b/app/assets/javascripts/discourse/controllers/login.js.es6
index 63326b8..19da455 100644
--- a/app/assets/javascripts/discourse/controllers/login.js.es6
+++ b/app/assets/javascripts/discourse/controllers/login.js.es6
@@ -60,13 +60,12 @@ export default Ember.Controller.extend(ModalFunctionality, {
 
   @computed("awaitingApproval", "hasAtLeastOneLoginButton")
   modalBodyClasses(awaitingApproval, hasAtLeastOneLoginButton) {
-    let classes = ["login-modal"];
+    const classes = ["login-modal"];
     if (awaitingApproval) classes.push("awaiting-approval");
     if (hasAtLeastOneLoginButton) classes.push("has-alt-auth");
     return classes.join(" ");
   },
 
-  // Determines whether at least one login button is enabled
   @computed("canLoginLocalWithEmail")
   hasAtLeastOneLoginButton(canLoginLocalWithEmail) {
     return findAll().length > 0 || canLoginLocalWithEmail;
@@ -84,10 +83,7 @@ export default Ember.Controller.extend(ModalFunctionality, {
     return canSignUp && !loggingIn && Ember.isEmpty(authenticate);
   },
 
-  @computed("loggingIn", "authenticate")
-  showSpinner(loggingIn, authenticate) {
-    return loggingIn || authenticate;
-  },
+  showSpinner: Ember.computed.or("loggingIn", "authenticate"),
 
   @computed("canLoginLocalWithEmail", "processingEmailLink")
   showLoginWithEmailLink(canLoginLocalWithEmail, processingEmailLink) {
@@ -96,16 +92,12 @@ export default Ember.Controller.extend(ModalFunctionality, {
 
   actions: {
     login() {
-      const self = this;
-      if (this.get("loginDisabled")) {
+      if (this.loginDisabled) {
         return;
       }
 
-      if (
-        Ember.isEmpty(this.get("loginName")) ||
-        Ember.isEmpty(this.get("loginPassword"))
-      ) {
-        self.flash(I18n.t("login.blank_username_or_password"), "error");
+      if (Ember.isEmpty(this.loginName) || Ember.isEmpty(this.loginPassword)) {
+        this.flash(I18n.t("login.blank_username_or_password"), "error");
         return;
       }
 
@@ -114,57 +106,66 @@ export default Ember.Controller.extend(ModalFunctionality, {
       ajax("/session", {
         type: "POST",
         data: {
-          login: this.get("loginName"),
-          password: this.get("loginPassword"),
-          second_factor_token: this.get("secondFactorToken"),
-          second_factor_method: this.get("secondFactorMethod")
+          login: this.loginName,
+          password: this.loginPassword,
+          second_factor_token: this.secondFactorToken,
+          second_factor_method: this.secondFactorMethod
         }
       }).then(
-        function(result) {
+        result => {
           // Successful login
           if (result && result.error) {
-            self.set("loggingIn", false);
+            this.set("loggingIn", false);
             if (
               result.reason === "invalid_second_factor" &&
-              !self.get("secondFactorRequired")
+              !this.secondFactorRequired
             ) {
-              $("#modal-alert").hide();
-              self.setProperties({
+              document.getElementById("modal-alert").style.display = "none";
+
+              this.setProperties({
                 secondFactorRequired: true,
                 showLoginButtons: false,
                 backupEnabled: result.backup_enabled,
                 showSecondFactor: true
               });
 
-              Ember.run.next(() => {
-                $("#second-factor input").focus();
-              });
+              Ember.run.schedule("afterRender", () =>
+                document
+                  .getElementById("second-factor")
+                  .querySelector("input")
+                  .focus()
+              );
 
               return;
             } else if (result.reason === "not_activated") {
-              self.send("showNotActivated", {
-                username: self.get("loginName"),
+              this.send("showNotActivated", {
+                username: this.loginName,
                 sentTo: escape(result.sent_to_email),
                 currentEmail: escape(result.current_email)
               });
             } else if (result.reason === "suspended") {
-              self.send("closeModal");
+              this.send("closeModal");
               bootbox.alert(result.error);
             } else {
-              self.flash(result.error, "error");
+              this.flash(result.error, "error");
             }
           } else {
-            self.set("loggedIn", true);
+            this.set("loggedIn", true);
             // Trigger the browser's password manager using the hidden static login form:
-            const $hidden_login_form = $("#hidden-login-form");
+            const hiddenLoginForm = document.getElementById(
+              "hidden-login-form"
+            );
+            const applyHiddenFormInputValue = (value, key) => {
+              if (!hiddenLoginForm) return;
+
+              hiddenLoginForm.querySelector(`input[name=${key}]`).value = value;
+            };
+
             const destinationUrl = $.cookie("destination_url");
             const ssoDestinationUrl = $.cookie("sso_destination_url");
-            $hidden_login_form
-              .find("input[name=username]")
-              .val(self.get("loginName"));
-            $hidden_login_form
-              .find("input[name=password]")
-              .val(self.get("loginPassword"));
+
+            applyHiddenFormInputValue(this.loginName, "username");
+            applyHiddenFormInputValue(this.loginPassword, "password");
 
             if (ssoDestinationUrl) {
               $.removeCookie("sso_destination_url");
@@ -173,39 +174,38 @@ export default Ember.Controller.extend(ModalFunctionality, {
             } else if (destinationUrl) {
               // redirect client to the original URL
               $.removeCookie("destination_url");
-              $hidden_login_form
-                .find("input[name=redirect]")
-                .val(destinationUrl);
+
+              applyHiddenFormInputValue(destinationUrl, "redirect");
             } else {
-              $hidden_login_form
-                .find("input[name=redirect]")
-                .val(window.location.href);
+              applyHiddenFormInputValue(window.location.href, "redirect");
             }
 
-            if (
-              navigator.userAgent.match(/(iPad|iPhone|iPod)/g) &&
-              navigator.userAgent.match(/Safari/g)
-            ) {
-              // In case of Safari on iOS do not submit hidden login form
-              window.location.href = $hidden_login_form
-                .find("input[name=redirect]")
-                .val();
-            } else {
-              $hidden_login_form.submit();
+            if (hiddenLoginForm) {
+              if (
+                navigator.userAgent.match(/(iPad|iPhone|iPod)/g) &&
+                navigator.userAgent.match(/Safari/g)
+              ) {
+                // In case of Safari on iOS do not submit hidden login form
+                window.location.href = hiddenLoginForm.querySelector(
+                  "input[name=redirect]"
+                ).value;
+              } else {
+                hiddenLoginForm.submit();
+              }
             }
             return;
           }
         },
-        function(e) {
+        e => {
           // Failed to login
           if (e.jqXHR && e.jqXHR.status === 429) {
-            self.flash(I18n.t("login.rate_limit"), "error");
+            this.flash(I18n.t("login.rate_limit"), "error");
           } else if (!areCookiesEnabled()) {
-            self.flash(I18n.t("login.cookies_error"), "error");
+            this.flash(I18n.t("login.cookies_error"), "error");
           } else {
-            self.flash(I18n.t("login.error"), "error");
+            this.flash(I18n.t("login.error"), "error");
           }
-          self.set("loggingIn", false);
+          this.set("loggingIn", false);
         }
       );
 

[... diff too long, it was truncated ...]

GitHub sha: 7aedc92a