Build an auth code input component that allows users to enter a 6-digit authorization code and submit it.

Auth code input example

Requirements

These are just the common 2FA basic behaviors, there are more edge cases to consider and we shan't list them all as it is a good practice for you to think through them on your own.

Here's a decent auth code input example.

Source

This question is adapted from FrontendEval's Two-factor Code Input question.

Solution

Solution

The complexity of an auth code input component lies in the user experience, the custom input handling, and focus management:

State

There aren't too many state variables necessary as the complexity lies in focus management.

Naturally, we will need state to store the code. Strictly speaking, uncontrolled inputs can be used but keeping the input values within state will enable us to enable/disable the "Reset" and "Submit" button appropriately.

It will be necessary to have finegrain control over which input field is focused, thus a focusedIndex state value will be helpful.