Skip to content

A platform for community discussion. Free, open, simple.

FIX: Focus OTP input after render (#40619)

Bryce Huhtalae0c200a2b4b7
diff --git a/frontend/discourse/app/ui-kit/d-otp/index.gjs b/frontend/discourse/app/ui-kit/d-otp/index.gjs
index b973919c779..aa1c41eb1af 100644
--- a/frontend/discourse/app/ui-kit/d-otp/index.gjs
+++ b/frontend/discourse/app/ui-kit/d-otp/index.gjs
@@ -4,10 +4,10 @@ import { tracked } from "@glimmer/tracking";
import { on } from "@ember/modifier";
import { action } from "@ember/object";
import { trackedArray } from "@ember/reactive/collections";
+import didInsert from "@ember/render-modifiers/modifiers/did-insert";
import { service } from "@ember/service";
import { isBlank } from "@ember/utils";
import preventScrollOnFocus from "discourse/modifiers/prevent-scroll-on-focus";
-import dAutoFocus from "discourse/ui-kit/modifiers/d-auto-focus";
import { i18n } from "discourse-i18n";
/** @type {import("./slot.gjs").default} */
import Slot from "./slot";
@@ -56,6 +56,23 @@ export default class DOTP extends Component {
return this.otp.join("");
}
+ @action
+ focusInput(element) {
+ if (!this.autoFocus) {
+ return;
+ }
+
+ element.autofocus = true;
+
+ requestAnimationFrame(() => {
+ if (!element.isConnected) {
+ return;
+ }
+
+ element.focus({ preventScroll: true });
+ });
+ }
+
/**
* @param {InputEvent!} event
*/
@@ -187,7 +204,7 @@ export default class DOTP extends Component {
{{on "blur" this.onBlur}}
{{on "paste" this.onPaste}}
aria-label={{i18n "d_otp.screen_reader" count=this.slots}}
- {{(if this.autoFocus (modifier dAutoFocus))}}
+ {{didInsert this.focusInput}}
...attributes
/>
</div>
diff --git a/frontend/discourse/tests/acceptance/second-factor-auth-test.js b/frontend/discourse/tests/acceptance/second-factor-auth-test.js
index 8b454813d5a..21526530bbd 100644
--- a/frontend/discourse/tests/acceptance/second-factor-auth-test.js
+++ b/frontend/discourse/tests/acceptance/second-factor-auth-test.js
@@ -1,4 +1,10 @@
-import { click, currentURL, fillIn, visit } from "@ember/test-helpers";
+import {
+ click,
+ currentURL,
+ fillIn,
+ visit,
+ waitUntil,
+} from "@ember/test-helpers";
import { test } from "qunit";
import { SECOND_FACTOR_METHODS } from "discourse/models/user";
import { acceptance } from "discourse/tests/helpers/qunit-helpers";
@@ -250,6 +256,20 @@ acceptance("Second Factor Auth Page", function (needs) {
);
});
+ test("TOTP input is focused on load", async function (assert) {
+ await visit("/session/2fa?nonce=ok110111");
+
+ await waitUntil(
+ () =>
+ document.activeElement ===
+ document.querySelector("form.totp-token .d-otp-input")
+ );
+
+ assert
+ .dom("form.totp-token .d-otp-input")
+ .isFocused("focuses the TOTP input");
+ });
+
test("error when submitting 2FA form", async function (assert) {
await visit("/session/2fa?nonce=ok110111");
await fillIn("form.totp-token .second-factor-token-input", WRONG_TOTP);
diff --git a/frontend/discourse/tests/integration/ui-kit/d-otp-test.gjs b/frontend/discourse/tests/integration/ui-kit/d-otp-test.gjs
index 858bc18c40e..a015fe309ee 100644
--- a/frontend/discourse/tests/integration/ui-kit/d-otp-test.gjs
+++ b/frontend/discourse/tests/integration/ui-kit/d-otp-test.gjs
@@ -1,5 +1,5 @@
import { next } from "@ember/runloop";
-import { focus, render, settled } from "@ember/test-helpers";
+import { focus, render, settled, waitUntil } from "@ember/test-helpers";
import { module, test } from "qunit";
import { setupRenderingTest } from "discourse/tests/helpers/component-test";
import DOtp from "discourse/ui-kit/d-otp";
@@ -83,6 +83,11 @@ module("Integration | ui-kit | DOtp", function (hooks) {
test("@autoFocus", async function (assert) {
await render(<template><DOtp /></template>);
+ await waitUntil(
+ () =>
+ document.activeElement === this.element.querySelector(".d-otp-input")
+ );
+
assert.dom(".d-otp-input").isFocused("autofocuses the input by default");
await render(<template><DOtp @autoFocus={{false}} /></template>);