hryvinskyi / magento2-customer-sign-in-frontend
Storefront pages of the customer sign-in suite: the second-step page, the Sign-in & Security account page, the redirects after a password sign-in that waits for a second step, recovery codes, and the layout containers sign-in methods plug into.
Package info
github.com/hryvinskyi/magento2-customer-sign-in-frontend
Type:magento2-module
pkg:composer/hryvinskyi/magento2-customer-sign-in-frontend
Requires
- php: ~8.1.0||~8.2.0||~8.3.0||~8.4.0||~8.5.0
- hryvinskyi/magento2-customer-sign-in: ^1.0
- hryvinskyi/magento2-customer-sign-in-api: ^1.0
- magento/framework: *
- magento/module-customer: *
- magento/module-store: *
- magento/module-theme: *
- psr/log: ^1.1 || ^2.0 || ^3.0
Requires (Dev)
- hryvinskyi/magento2-clock: ^1.0
- hryvinskyi/magento2-customer-sign-in-email: ^1.0
- hryvinskyi/magento2-customer-sign-in-passkey: ^1.0
- hryvinskyi/magento2-customer-sign-in-sms: ^1.0
- hryvinskyi/magento2-customer-sign-in-totp: ^1.0
- hryvinskyi/magento2-webauthn: ^1.0
- magento/module-sales: *
Suggests
- hryvinskyi/magento2-customer-sign-in-email: needed only to run this package's browser specs
- hryvinskyi/magento2-customer-sign-in-passkey: needed only to run this package's account deletion test and browser specs
- hryvinskyi/magento2-customer-sign-in-sms: needed only to run this package's account deletion test
- hryvinskyi/magento2-customer-sign-in-totp: needed only to run this package's account deletion test and browser specs
- magento/module-sales: needed only to run this package's integration tests
Provides
None
Conflicts
None
Replaces
None
README
The storefront half of the customer sign-in suite. It adds two pages: Finish signing in, where a customer whose account has a second step finishes a password sign-in, and Sign-in & Security in My Account, where a customer manages their sign-in methods and recovery codes. It also asks for a recent sign-in before a customer changes their methods, sends a signed-in customer who owes the second step the store requires to Sign-in & Security to set one up, and gives the sign-in page a place for other ways to sign in. It adds no sign-in method of its own: the method packages (email code, passkey, authenticator app, text message) place their parts on these pages.
What the customer sees
Other ways to sign in
On the sign-in page, right under the sign-in form's Sign In button and Forgot Your Password? link, a thin rule with the word or on it (in capitals), then one button for each sign-in method that offers one (Email me a sign-in code, Sign in with a passkey). The buttons stand side by side on screens 768 pixels wide and wider, and stacked at full width on narrower ones. A method may show a message below the buttons, such as why a passkey did not sign in.
The section shows while at least one of its buttons shows (a passkey button shows only on a browser with passkeys), and only once the page's script has run: without JavaScript it stays hidden. Pressing Enter in the sign-in form signs in with the password.
Finish signing in
A customer whose account has a second step (a passkey, an authenticator app or a phone) enters their email and password as usual. When the password is right, they are not signed in yet:
- From the sign-in page, they go to the Finish signing in page.
- From the sign-in pop-up, they go to the same page, which says Finish signing in with your second step.
The page shows:
You have <N> attempts left., once an entry did not work.- One part for each of the customer's second-step methods: the code form of the authenticator app, Send a code to … for a phone, Use a passkey for a passkey. Each method package adds its own part.
- Use a recovery code, with the field Recovery code and the button Sign in with a recovery code, while the customer has recovery codes.
- Cancel, which ends the waiting sign-in and returns to the sign-in page.
After the second step the customer goes where the store's own sign-in would have sent them: back to the page that asked them to sign in, or to My Account when Redirect Customer to Account Dashboard after Logging in (Customers > Customer Configuration > Login Options) is Yes. A visitor who opens the page with no sign-in waiting goes to the sign-in page.
A step that does not work shows one of these messages:
| Message | When | The customer goes to |
|---|---|---|
That did not work. Try again. |
A wrong entry, with attempts left. | Finish signing in |
There were too many wrong entries. Sign in again. |
The attempts of this sign-in are used up. | The sign-in page |
There were too many wrong entries for this account. Try again later. |
The account reached its wrong entries for the hour, across all its sign-ins. | Finish signing in |
Your sign-in has ended. Sign in again. |
No sign-in waits any more: it was cancelled, or the time limit for the second step passed. | The sign-in page |
This sign-in method cannot finish this sign-in. |
The method is not one of this customer's. | Finish signing in |
This sign-in method is not available here. |
The method cannot do this step: it cannot answer a second step, or it is switched off and cannot start a sign-in. | Finish signing in, or the sign-in page when no sign-in waits |
This sign-in was started on another website. Sign in again. |
The sign-in began on another website of the store. | The sign-in page |
Enter one of your recovery codes. |
The recovery-code form was sent empty. | Finish signing in |
Sign-in & Security
My Account lists Sign-in & Security right after Account Information. The page opens with Choose how you sign in to your account. To add or remove a method you need to have signed in recently., then one section for each installed sign-in method, then Recovery codes. A visitor who is not signed in goes to the sign-in page and comes back here after signing in.
- Removing a method. Each method section has its own button (Remove, Turn off). The page then shows
<name> was removed., where the name is the passkey's name, the masked phone number or Authenticator app. Recovery codes have no remove button: they go with the customer's last second-step method. While the store requires a second step from the customer's group, the last second-step method stays: the page showsYour store requires a second sign-in step. Add another method before you remove this one. - A second step the store requires. While the customer owes one (see the next section), the page opens with the notice
Your store requires a second sign-in step for your account. Set one up below to continue.An administrator signed in as the customer sees it too. - Recovery codes. A customer gets ten codes with their first passkey, authenticator app or phone, and loses them with their last one. The section shows only to a customer who has a second step:
Unused recovery codes: <N>and the button Create new recovery codes, with the noteNew codes replace your current ones, which stop working. - New recovery codes show once, under
Save these recovery codes somewhere safe. Each code works once. They will not be shown again.After Create new recovery codes the page also saysNew recovery codes were created. Your earlier codes no longer work. - A recent sign-in. Adding or removing a method and creating new recovery codes need a sign-in within the last 15 minutes (the Recent Sign-in Window setting). A customer who signed in earlier is signed out and sent to the sign-in page with
For your security, sign in again to change your sign-in methods.After signing in, they come back to Sign-in & Security. A password sign-in and a sign-in with one of the suite's methods both count. - Emails. The customer gets an email when a method is added or removed and when a recovery code is used, while Email Customers About Changes is Yes.
- Other failures. A failure that is not a refusal shows
Something went wrong. Try again.and is written to the store's exception log.
A second step the store requires
A store can require a second step from the customers of chosen groups (Require a Second Step, see Configuration). A signed-in customer of such a group who has no passkey, authenticator app or phone owes one. Until they add one:
- Every page that asks the customer session to authenticate the customer sends them to Sign-in & Security. That is every page of My Account that the platform adds: the account itself, addresses, orders, the wish list, reviews, stored payment methods, downloads, newsletter subscriptions and billing agreements, and the page of any other module that asks the same. A few catalog and cart actions ask the same and are held back too: adding to the wish list, moving an item from the cart to the wish list, price and stock alerts, the multiple-address checkout, and emailing a product to a friend where guests may not send.
- The checkout page sends them to Sign-in & Security when
hryvinskyi/magento2-customer-sign-in-checkoutis installed, since it does not ask the customer session. - Never held back: Sign-in & Security with every method's set-up page (the whole
customersigninroute), signing out, and a session an exemption lets through: an administrator signed in as the customer, whenhryvinskyi/magento2-customer-sign-in-login-as-customeris installed. - Everything else works: the catalog, the cart and content pages, and any page that checks the customer another way than by asking the session to authenticate them.
Once the customer has added a passkey, an authenticator app or a phone, every page opens again.
Configuration
The package has no settings of its own. These settings of the sign-in engine decide what the two pages do. They are under Stores > Settings > Configuration > Customers > Sign-in Methods > General, and the README of hryvinskyi/magento2-customer-sign-in describes the whole group.
| Setting | Path | Scope | Default | Effect here |
|---|---|---|---|---|
| Second Step Time Limit (minutes) | customer_sign_in/general/pending_lifetime |
Website | 10 |
How long Finish signing in waits after the password. |
| Second Step Attempts | customer_sign_in/general/pending_attempts |
Website | 5 |
The <N> of You have <N> attempts left. for a new sign-in. |
| Wrong Second Step Entries per Hour | customer_sign_in/general/verify_failures_per_hour |
Website | 10 |
Wrong entries one customer may make in an hour, across all their sign-ins. |
| Recent Sign-in Window (minutes) | customer_sign_in/general/recent_sign_in_window |
Website | 15 |
How recent a sign-in must be to change methods or create new recovery codes. |
| Email Customers About Changes | customer_sign_in/general/notify |
Store view | 1 (Yes) |
Whether the emails about added and removed methods and used recovery codes go out. |
| Require a Second Step | customer_sign_in/general/require_second_step |
Website | 0 (No) |
Yes: a customer of the chosen groups who has no second step is sent to Sign-in & Security and sees its notice. |
| Customer Groups That Need a Second Step | customer_sign_in/general/require_second_step_groups |
Website | none | The groups the requirement covers; with none chosen it covers nobody. The requirement also covers nobody while no passkey, authenticator app or text message method is switched on for the website. |
The package has no switch. A customer without a second step never sees Finish signing in. Sign-in & Security stays in My Account when every method is switched off; what each method's section shows then is in that method package's README.
Requirements
- Magento Open Source or Adobe Commerce 2.4.x with a Luma-based theme. The suite is built and tested on 2.4.9.
- PHP 8.1 to 8.5.
hryvinskyi/magento2-customer-sign-in-api^1.0 andhryvinskyi/magento2-customer-sign-in^1.0.- The platform's Customer, Store and Theme modules.
Installation
Run from the Magento root. hryvinskyi/magento2-customer-sign-in-pack installs this package with the rest of the suite.
composer require hryvinskyi/magento2-customer-sign-in-frontend bin/magento module:enable Hryvinskyi_Clock Hryvinskyi_RateLimit Hryvinskyi_VerificationCode Hryvinskyi_CustomerSignInApi Hryvinskyi_CustomerSignIn Hryvinskyi_CustomerSignInFrontend bin/magento setup:upgrade bin/magento setup:di:compile # production mode only bin/magento setup:static-content:deploy -f # production mode only bin/magento cache:flush
Verify on the storefront: open the sign-in page; with a method package installed, its button shows under Sign In. Sign in as a customer and open My Account. Sign-in & Security is listed after Account Information. Until a method package is installed, the page shows only its opening sentence.
Limits
- Only the password sign-in and the email code lead to the second step. A module that signs a customer in without the platform's password check, such as a social login, opens the session with no second step.
- The sign-in form and the sign-in pop-up go to the second step. Another page that checks a password and opens a session leaves the visitor signed out when the account has a second step. The private RSS feeds, which sign in by HTTP authentication, answer
401for such a customer. - "Remember Me" needs
hryvinskyi/magento2-customer-sign-in-persistent. Without it, a customer who ticked Remember Me is remembered while their second step is still due. With it, a sign-in that waits for its second step is remembered only when the box was ticked on the sign-in page, once the second step is answered. - The other ways to sign in need JavaScript. Without it the sign-in page shows only the password sign-in; a method that has a page of its own (the email code's
customersignin/email/start) stays reachable by its address. - Hyvä themes are not supported. The pages and the method parts are Luma templates.
- Held back means a refused authentication. A page that asks the customer session to authenticate the customer is told no, with the session's redirect pointed at Sign-in & Security. A page that only stops on that answer is held back cleanly. A page that also does its guest work on it does that work for this signed-in customer too: adding to the wish list shows
You must login or register to add items to your wishlist.and keeps the request to repeat after a sign-in, and the multiple-address checkout may send the customer to the cart instead of Sign-in & Security. - Only the pages that ask are held back. The checkout page needs
hryvinskyi/magento2-customer-sign-in-checkout, and it holds back only the one-page checkout page. An order placed without opening that page is not stopped: an express payment button that starts a payment provider's own review flow, one-click purchase on the product page, and a web API call the storefront makes with the customer's session. A page of another module that checks the customer another way needs that module to askModel\SecondStepSetUpGateInterface(see Extension points).
Registrations
| Where | What |
|---|---|
| Storefront route | frontName customersignin: verify/index (GET), verify/cancel (POST), recovery/verify (POST, code), recovery/regenerate (POST), security/index (GET), security/remove (POST, factor_id). Every POST carries the form key. |
| Layout handles | customersignin_verify_index, customersignin_security_index, customer_account (the account link customer-account-navigation-sign-in-security-link, sort order 175) and customer_account_login (the block customer.signin.login.alternatives.section, last in the sign-in form's form.additional.info, cacheable). |
| Containers | customer.signin.verify.methods on Finish signing in; customer.signin.security.sections on Sign-in & Security; customer.signin.login.alternatives on the sign-in page. |
| Templates | verify.phtml, security.phtml (both blocks cacheable="false"), login/alternatives.phtml. |
| Script and styles | js/login-alternatives moves the sign-in page's section to right after the form's actions toolbar and shows it. web/css/source/_module.less styles the section; Luma and Blank based themes compile it. |
| Plugins (storefront) | after on Magento\Customer\Controller\Account\LoginPost::execute and Magento\Customer\Controller\Ajax\Login::execute: a request that parked a sign-in for its second step answers with the Finish signing in page. after on Magento\Customer\Model\Session::authenticate (Plugin\Customer\SendOwedSecondStepToSetUp): answers false and redirects to customersignin/security when Model\SecondStepSetUpGateInterface::mustSetUpFirst() is true. |
| Gate | Preference Model\SecondStepSetUpGateInterface → Model\SecondStepSetUpGate (global etc/di.xml). Its arguments, in etc/frontend/di.xml only: openRequests (customer_sign_in → customersignin, sign_out → customer_account_logout) and exemptions (empty). |
The package registers no table, cron job, command, setting or ACL resource.
Extension points
A sign-in method module adds its controllers to the customersignin route and its parts to the containers. The method itself joins the engine's method pool (see the README of hryvinskyi/magento2-customer-sign-in-api).
1. Controllers on the route. Declare the module before this one, so its controllers answer first:
<router id="standard"> <route id="customersignin" frontName="customersignin"> <module name="Vendor_SignInMethod" before="Hryvinskyi_CustomerSignInFrontend"/> </route> </router>
2. A part on Finish signing in. Add a block to customer.signin.verify.methods in the handle customersignin_verify_index. ViewModel\Pending gives the waiting customer's factors of one method. Show the part whenever the customer has a factor of the method, switched on or not: a customer who has a second step is still asked for it. This is the authenticator app's entry:
<referenceContainer name="customer.signin.verify.methods"> <block class="Magento\Framework\View\Element\Template" name="customer.signin.verify.totp" template="Hryvinskyi_CustomerSignInTotp::verify.phtml"> <arguments> <argument name="pending_view_model" xsi:type="object">Hryvinskyi\CustomerSignInFrontend\ViewModel\Pending</argument> <argument name="app_view_model" xsi:type="object">Hryvinskyi\CustomerSignInTotp\ViewModel\AuthenticatorApp</argument> </arguments> </block> </referenceContainer>
3. A section on Sign-in & Security. Add a block to customer.signin.security.sections in the handle customersignin_security_index. ViewModel\Security lists the customer's factors of one method. A section removes a factor by posting factor_id and form_key to getRemoveUrl(), which removes a factor of any method after the recent-sign-in check.
4. An action among the sign-in page's other ways to sign in. Add a block to customer.signin.login.alternatives in the handle customer_account_login, and let it print nothing where the method is switched off: the section is printed only when one of its actions prints something, and shows while one element with the class action in it is not hidden. Name no other method's block; before="-" and after="-" place an action first or last. The email code's entry:
<referenceContainer name="customer.signin.login.alternatives"> <block class="Magento\Framework\View\Element\Template" name="customer.signin.email.button" template="Hryvinskyi_CustomerSignInEmail::login-button.phtml" before="-"> <arguments> <argument name="view_model" xsi:type="object">Hryvinskyi\CustomerSignInEmail\ViewModel\EmailSignIn</argument> </arguments> </block> </referenceContainer>
What an action may rely on:
- It is printed inside the sign-in form, hidden, and shown under the form's actions toolbar by the section's script. A button of type
submitthere would send the sign-in form, and until the script has moved the section it would come before Sign In as the button Enter presses: a button that sends a form of its own names that form in itsformattribute, and the form itself is printed outside the sign-in form (inbefore.body.end, not incustomer.login.container, whose children the theme lays out as columns). A form nested inside the sign-in form is not valid HTML. - The buttons use the theme's
action secondaryclasses. The section lays out each child element as one action. An element with the classcustomer-signin-alternatives-grouplays out its own children as actions instead; one with the classcustomer-signin-alternatives-linetakes a line of its own below the actions, for a message. - A part printed hidden carries no class, since a theme's rule for a class outweighs the
hiddenattribute; the script that shows it puts the class on. - The page is cacheable. An action prints wording and addresses only, never a visitor's data.
5. The services a controller uses. All are @api:
| Service | Use |
|---|---|
Model\StepUpResponderInterface::respond(): ?ResultInterface |
Call first in every action that adds a factor. null lets the change go on; a result signs the customer out with the step-up notice, and the action returns it unchanged. |
Model\OutcomeResponderInterface |
respond() answers a SignInOutcomeInterface with a redirect, respondJson() with {ok, redirectUrl} or {ok: false, message, redirectUrl?} for a page script. A finished sign-in goes where the platform's sign-in goes; a due second step goes to Finish signing in; a refusal shows its message. |
Model\RefusalMessagesInterface::show(\Throwable) |
Shows a SignInRefusedException in its own words, and any other failure as Something went wrong. Try again., logged. |
Model\NewRecoveryCodesFlashInterface |
put() the recovery codes an enrolment returned, then redirect to customersignin/security, which shows them once. |
ViewModel\Pending |
getFactorsOfMethod(), getAttemptsLeft(), hasFailedAttempts(), hasRecoveryCodes() for the waiting sign-in. |
ViewModel\Security |
getFactorsOfMethod(), isRecent(), getRemoveUrl(), getFormKey() for the signed-in customer. |
The authenticator app's setup confirms its code this way (Hryvinskyi\CustomerSignInTotp\Controller\Totp\Confirm): the step-up responder first, then the enrolment, the recovery codes into the flash, and back to Sign-in & Security.
public function execute(): ResultInterface { $stepUp = $this->stepUp->respond(); if ($stepUp !== null) { return $stepUp; } // ... reads the customer and the secret of the setup ... return $this->turnOn($customerId, $secret); } private function turnOn(int $customerId, string $secret): ResultInterface { $code = $this->request->getParam('code'); try { $result = $this->enrolment->turnOn($customerId, $secret, is_string($code) ? $code : ''); } catch (LocalizedException $failure) { $this->refusals->show($failure); return $this->redirects->create()->setPath(self::SECURITY_PAGE); } // ... a wrong code returns to the setup page ... $this->setup->finish(); $this->newCodes->put($result->getRecoveryCodes() ?? []); $this->messages->addSuccessMessage((string)$this->notices->turnedOn()); return $this->redirects->create()->setPath(self::SECURITY_PAGE); }
6. Hold back a page that does not ask the customer session. A page that needs a signed-in customer but checks it another way asks Model\SecondStepSetUpGateInterface (@api) before it runs, and redirects to setUpUrl() when mustSetUpFirst() is true. mustSetUpFirst() is true when a customer is signed in, the request is not in the open list, no exemption applies, and the customer owes a second step on the website of the request. hryvinskyi/magento2-customer-sign-in-checkout does this for the checkout page with an around plugin:
public function aroundExecute(Index $subject, callable $proceed): ResultInterface { if (!$this->gate->mustSetUpFirst()) { return $proceed(); } return $this->redirects->create()->setUrl($this->gate->setUpUrl()); }
7. Keep a page open, or let a session through. Model\SecondStepSetUpGate takes two array arguments. Add items to them only in your module's etc/frontend/di.xml: the gate's lists are declared there, and an area di.xml replaces an array argument of the global one, so an item in a global di.xml is lost on the storefront.
openRequests: requests that are never held back. An item is a full action name (route_controller_action), a route, or a route and controller (route_controller). It matches a request whose full action name equals it or starts with it followed by_.exemptions: objects implementingModel\SetUpExemptionInterface(@api). When one'sapplies()answerstrue, the signed-in session is not the customer acting for themselves, and nothing is held back.
<type name="Hryvinskyi\CustomerSignInFrontend\Model\SecondStepSetUpGate"> <arguments> <argument name="openRequests" xsi:type="array"> <item name="vendor_preferences" xsi:type="string">vendorroute_preferences</item> </argument> <argument name="exemptions" xsi:type="array"> <item name="vendor_support_session" xsi:type="object">Vendor\Module\Model\SupportAgentSession</item> </argument> </arguments> </type>
Vendor\Module\Model\SupportAgentSession, the route vendorroute and its controller preferences stand for your module's own. The suite's bridges use both lists: hryvinskyi/magento2-customer-sign-in-privacy-consent adds an open request, and hryvinskyi/magento2-customer-sign-in-login-as-customer an exemption.
An openRequests item that is not a non-empty string, or an exemptions item that does not implement SetUpExemptionInterface, makes the gate refuse to be built with an InvalidArgumentException that names the item.
Tests
Run from the Magento root.
- Unit:
vendor/bin/phpunit -c app/code/Hryvinskyi/CustomerSignInFrontend/phpunit.xml.dist. - Browser script (Node 22.15 or later): in
app/code/Hryvinskyi/CustomerSignInFrontend/Test/Js,npm ci, thennpm testandnpm run check(the type check). - Integration: the platform's harness in
dev/tests/integration, against its own database, one run at a time.cd dev/tests/integration && php -d memory_limit=4G ../../../vendor/bin/phpunit -c phpunit.xml ../../../app/code/Hryvinskyi/CustomerSignInFrontend/Test/Integration. The integration tests also need the platform's Sales module, and the account deletion test needs the passkey, authenticator app and text message packages. - Browser flows:
Test/E2e/, run with the shop's Playwright project ine2e/(npm testthere). - Static analysis: PHPStan 2.x at level 10 with the package's
phpstan.neon.dist(phpstan analyse -c app/code/Hryvinskyi/CustomerSignInFrontend/phpstan.neon.dist --level=10), andvendor/bin/phpcs --standard=app/code/Hryvinskyi/CustomerSignInFrontend/phpcs.xml.dist app/code/Hryvinskyi/CustomerSignInFrontend.
Translations
The storefront wording ships in en_US and 25 more locales: the official languages of the European Union, Norwegian Bokmål and Ukrainian. The translations are machine-made and no native speaker has read them. Have them checked before a store relies on them, and override a phrase in a theme or a language pack.