A platform for community discussion. Free, open, simple.
FIX: Submit password logins through the login form (#43707)
Keegan George29c35dd9ebd1
diff --git a/frontend/discourse/app/components/local-login-form.gjs b/frontend/discourse/app/components/local-login-form.gjsindex b890e580042..7bfb4d158cb 100644--- a/frontend/discourse/app/components/local-login-form.gjs+++ b/frontend/discourse/app/components/local-login-form.gjs@@ -125,10 +125,9 @@ export default class LocalLoginForm extends Component {}@action- loginOnEnter(event) {- if (event.key === "Enter") {- this.args.login();- }+ submit(event) {+ event.preventDefault();+ this.args.login();}@action@@ -182,7 +181,7 @@ export default class LocalLoginForm extends Component {}<template>- <form id="login-form" method="post">+ <form id="login-form" method="post" novalidate {{on "submit" this.submit}}><div class={{this.credentialsClass}} id="credentials"><div class="input-group" {{didInsert this.passkeyConditionalLogin}}><Input@@ -193,12 +192,12 @@ export default class LocalLoginForm extends Component {class={{valueEntered @loginName}}disabled={{@showSecondFactor}}id="login-account-name"+ inputmode="email"tabindex="1"- @type="email"+ @type="text"@value={{@loginName}}{{on "focusin" this.scrollInputIntoView}}{{on "input" @loginNameChanged}}- {{on "keydown" this.loginOnEnter}}/><label class="alt-placeholder" for="login-account-name">{{i18n "login.email_placeholder"}}@@ -238,7 +237,6 @@ export default class LocalLoginForm extends Component {value={{@loginPassword}}@capsLockOn={{this.capsLockOn}}{{on "focusin" this.scrollInputIntoView}}- {{on "keydown" this.loginOnEnter}}{{on "input" @loginPasswordChanged}}/><label class="alt-placeholder" for="login-account-password">@@ -291,7 +289,6 @@ export default class LocalLoginForm extends Component {@onChange={{fn (mut @secondFactorToken)}}@onFill={{this.filledSecondFactorToken}}@secondFactorMethod={{@secondFactorMethod}}- {{on "keydown" this.loginOnEnter}}{{on "focusin" this.scrollInputIntoView}}/>{{/if}}diff --git a/frontend/discourse/app/components/login-page-cta.gjs b/frontend/discourse/app/components/login-page-cta.gjsindex 2bc0e8cc8fe..1594325bf6b 100644--- a/frontend/discourse/app/components/login-page-cta.gjs+++ b/frontend/discourse/app/components/login-page-cta.gjs@@ -12,10 +12,10 @@ const LoginPageCta = <template>form="login-form"id="login-button"tabindex={{unless @showSecondFactor "2"}}- @action={{@login}}@disabled={{@loginDisabled}}@isLoading={{@loggingIn}}@label={{@loginButtonLabel}}+ @type="submit"/>{{/unless}}diff --git a/frontend/discourse/app/templates/login.gjs b/frontend/discourse/app/templates/login.gjsindex 7d9c0fd6154..cadd334d1e1 100644--- a/frontend/discourse/app/templates/login.gjs+++ b/frontend/discourse/app/templates/login.gjs@@ -125,7 +125,6 @@ export default <template>@canLoginLocal={{@controller.canLoginLocal}}@createAccount={{@controller.createAccount}}@loggingIn={{@controller.loggingIn}}- @login={{@controller.localLogin}}@loginButtonLabel={{@controller.loginButtonLabel}}@loginDisabled={{@controller.loginDisabled}}@showSecondFactor={{@controller.showSecondFactor}}@@ -170,7 +169,6 @@ export default <template>@canLoginLocal={{@controller.canLoginLocal}}@createAccount={{@controller.createAccount}}@loggingIn={{@controller.loggingIn}}- @login={{@controller.localLogin}}@loginButtonLabel={{@controller.loginButtonLabel}}@loginDisabled={{@controller.loginDisabled}}@showSecondFactor={{@controller.showSecondFactor}}diff --git a/frontend/discourse/tests/acceptance/sign-in-test.js b/frontend/discourse/tests/acceptance/sign-in-test.jsindex 40762e73e9b..221d4abd808 100644--- a/frontend/discourse/tests/acceptance/sign-in-test.js+++ b/frontend/discourse/tests/acceptance/sign-in-test.js@@ -1,8 +1,86 @@-import { click, fillIn, visit } from "@ember/test-helpers";+import {+ click,+ fillIn,+ find,+ settled,+ triggerKeyEvent,+ visit,+} from "@ember/test-helpers";import { test } from "qunit";+import pretender, {+ parsePostData,+} from "discourse/tests/helpers/create-pretender";import { acceptance } from "discourse/tests/helpers/qunit-helpers";-acceptance("Signing In", function () {+function sessionRequests() {+ return pretender.handledRequests.filter(+ ({ method, url }) => method === "POST" && url.endsWith("/session")+ );+}++acceptance("Signing In", function (needs) {+ needs.settings({ enable_local_logins_via_code: true });++ test("submits username credentials with the login button", async function (assert) {+ await visit("/login");++ assert+ .dom("#login-account-name")+ .hasAttribute("type", "text", "accepts usernames")+ .hasAttribute("inputmode", "email", "uses an email-friendly keyboard");+ assert+ .dom("#login-button")+ .hasAttribute("type", "submit", "is a native submit button")+ .hasAttribute("form", "login-form", "submits the login form");++ await fillIn("#login-account-name", "eviltrout");+ await fillIn("#login-account-password", "incorrect");+ await click("#login-button");++ const requests = sessionRequests();+ assert.strictEqual(requests.length, 1, "sends one login request");+ assert.strictEqual(+ parsePostData(requests[0].requestBody).login,+ "eviltrout",+ "submits the username"+ );+ assert.dom(".code-login-form").doesNotExist("keeps the password flow");+ });++ test("submits email credentials once when pressing Enter", async function (assert) {+ await visit("/login");+ await fillIn("#login-account-name", "[email protected]");+ await fillIn("#login-account-password", "incorrect");++ await triggerKeyEvent("#login-account-password", "keydown", "Enter");+ // Synthetic key events do not perform the browser's default form submission.+ find("#login-form").requestSubmit();+ await settled();++ const requests = sessionRequests();+ assert.strictEqual(requests.length, 1, "sends one login request");+ assert.strictEqual(+ parsePostData(requests[0].requestBody).login,+ "[email protected]",+ "submits the email address"+ );+ assert.dom(".code-login-form").doesNotExist("keeps the password flow");+ });++ test("opens code login only from its explicit link", async function (assert) {+ await visit("/login");++ await click("#one-time-code-link");++ assert.dom("#login-form").doesNotExist("hides the password form");+ assert.dom(".code-login-form").exists("shows the code login form");+ assert.strictEqual(+ sessionRequests().length,+ 0,+ "does not try password login"+ );+ });+test("sign in", async function (assert) {await visit("/");await click("header .login-button");