hryvinskyi / magento2-customer-sign-in-sms
Text message sign-in method for the customer sign-in suite: a customer adds one phone number from Sign-in & Security, confirms it with a texted code, and afterwards answers the second step of a sign-in with a code texted to that number.
Package info
github.com/hryvinskyi/magento2-customer-sign-in-sms
Type:magento2-module
pkg:composer/hryvinskyi/magento2-customer-sign-in-sms
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
- hryvinskyi/magento2-customer-sign-in-frontend: ^1.0
- hryvinskyi/magento2-rate-limit: ^1.1
- hryvinskyi/magento2-sms: ^1.0
- hryvinskyi/magento2-verification-code: ^1.0
- magento/framework: *
- magento/module-config: *
- magento/module-customer: *
- magento/module-store: *
Requires (Dev)
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
A sign-in method of the customer sign-in suite. A customer adds their phone number in Sign-in & Security. From then on, every sign-in with a password or an email code asks for a 6-digit code texted to that phone. A phone cannot sign a customer in alone: it is a second step only.
The package sends no text message by itself. It sends through the transport chosen under Stores > Settings > Configuration > General > Text Messages (hryvinskyi/magento2-sms), and the only transport that ships, Log file (development only), reaches no phone. The method is off by default. Install a transport module for an SMS provider, choose it there, and only then switch the method on.
What the customer sees
Add a phone
My Account > Sign-in & Security has a Text message section: Finish signing in with a code we text to your phone after you enter your password. and the button Add a phone number.
The page Text Message asks for the number:
- In Phone number, the customer enters their number with its country code (
Include the country code, for example +45 12 34 56 78.) and clicks Send code. The page showsWe sent a code to <masked number>. - Under
Enter the code we sent to <masked number>., they enter the code in Code and click Add phone number. Use another number with Send code starts again with a different number.
The customer returns to Sign-in & Security with Text message sign-in is on. When this is their first second step, the page shows their ten new recovery codes once, to save. The customer gets an email that a sign-in method was added.
The section then lists the phone by its masked number, such as +45 •••• 5678, with Added on <date> and a Remove button. A customer has one phone at most.
Sending the code and adding the phone need a recent sign-in (hryvinskyi/magento2-customer-sign-in-frontend).
Sign in with a text message
After the password or the email code, the Finish signing in page shows a Text message part for the phone: the button Send a code to , the field Code from the text message and the button Sign in. The text reads <code> is your <store name> sign-in code. A customer without the phone uses a recovery code on the same page.
Remove the phone
Remove in the section removes the phone after a recent sign-in, and the customer gets an email. When it was the customer's last second step, the recovery codes go with it, and the customer signs in with their password alone. While the store requires a second step from the customer's group, the customer cannot remove their last second step: the page shows Your store requires a second sign-in step. Add another method before you remove this one.
Messages
| Message | Cause |
|---|---|
Enter the phone number with its country code, for example +45 12 34 56 78. |
The number could not be read as a phone number. |
That code is not right. You have <N> attempts left. |
A wrong code while adding the phone. A code allows 5 entries. |
This code no longer works. Request a new code. |
The code expired, has no entries left, or is unknown. |
Enter the code from the text message. |
The code form was sent empty. |
Too many text messages were requested. Try again later. |
More than 5 texts to one customer, or 20 from one client address, in an hour. |
Text messages cannot be sent right now. Try again later. |
The store's transport refused the text while adding a phone. |
Text messages cannot be sent right now. Choose another way to sign in. |
The store's transport refused the text on Finish signing in. |
You already sign in with a text message. Remove that phone number first to add another. |
Add a phone number for a customer who has a phone. |
Text message sign-in is not available. |
Add a phone number while the method is switched off for the website. |
A code cannot be texted for this sign-in. |
A text requested for a method that is not this customer's phone. |
A wrong code on Finish signing in shows that page's own messages, listed in the README of hryvinskyi/magento2-customer-sign-in-frontend.
Configuration
Stores > Settings > Configuration > Customers > Sign-in Methods > Text Message. The section needs the role resource Stores > Settings > Configuration > Customer Sign-in Methods.
| Setting | Path | Scope | Default | Effect |
|---|---|---|---|---|
| Enabled | customer_sign_in/sms/enabled |
Website | 0 (No) |
Lets customers add a phone. Switch it on only after choosing a transport that delivers text messages under Text Messages. |
| Code Lifetime (minutes) | customer_sign_in/sms/code_lifetime |
Website | 5 |
How long a texted code works. |
The transport is chosen in Stores > Settings > Configuration > General > Text Messages > General > Transport (sms/general/transport, default log). The README of hryvinskyi/magento2-sms describes it and how a module adds a transport.
When it is switched off
On that website a customer cannot add a phone: the section is hidden from a customer who has none, and Add a phone number is refused. A customer who already added a phone still sees it in the section, can remove it, and is still asked for a texted code on Finish signing in. The texts still go out through the transport.
Security behaviour
- Codes. Six digits, 5 entries each, valid for Code Lifetime (minutes). A code texted for the second step answers only for the phone it was texted to.
- Texts per hour. At most 5 texts to one customer and 20 from one client address in any hour, for adding a phone and for signing in together.
- A text that fails. When the transport cannot send, no code that works is kept, and the customer reads that text messages cannot be sent.
- Wrong codes at sign-in. The attempts and the hourly limit of Finish signing in apply (Second Step Attempts, Wrong Second Step Entries per Hour under Sign-in Methods > General).
- The number. The full number is stored in E.164 with the phone; the pages, the texts' confirmations and the admin tab show it masked. It is deleted with the phone, and so with the customer.
Limits
- No real transport ships. Log file (development only) writes each text to
var/log/sms.login developer mode only, and refuses every text in any other mode. With it, adding a phone fails withText messages cannot be sent right now. Try again later., and a customer who already has a phone gets no code on Finish signing in and finishes with a recovery code. - One phone per customer.
- Only the password sign-in and the email code ask for the phone. A module that signs a customer in without the platform's password check opens the session with no second step. Customer tokens of the REST and GraphQL APIs are refused for a customer with a phone (see
hryvinskyi/magento2-customer-sign-in). - Codes stay stored after an account is deleted, for up to 24 hours. They hold no number, are hashed and bound to the customer or the phone, and the daily clean-up of
hryvinskyi/magento2-verification-codedeletes them. - Hyvä themes are not supported. The section, the setup page and the second-step part are Luma templates.
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,-customer-sign-inand-customer-sign-in-frontend^1.0,hryvinskyi/magento2-sms^1.0,hryvinskyi/magento2-verification-code^1.0 andhryvinskyi/magento2-rate-limit^1.1.- An SMS transport module for
hryvinskyi/magento2-sms, for production use. - The platform's Config, Customer and Store 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-sms bin/magento module:enable Hryvinskyi_Clock Hryvinskyi_RateLimit Hryvinskyi_VerificationCode Hryvinskyi_Sms Hryvinskyi_CustomerSignInApi Hryvinskyi_CustomerSignIn Hryvinskyi_CustomerSignInFrontend Hryvinskyi_CustomerSignInSms 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
Then choose the transport and switch the method on, here for the default scope:
bin/magento config:set sms/general/transport <transport_code> bin/magento config:set customer_sign_in/sms/enabled 1 bin/magento cache:flush
<transport_code> is the code of the installed transport module's transport. On a development copy in developer mode, log writes the texts to var/log/sms.log.
Verify on the storefront: sign in as a customer and open Sign-in & Security. The Text message section shows Add a phone number. Add a phone, sign out, sign in with the password, and click Send a code to … on Finish signing in: the text arrives (or, with log, a line is written to var/log/sms.log).
Registrations
| Where | What |
|---|---|
| Method pool | Item sms in the methods argument of Hryvinskyi\CustomerSignIn\Model\MethodPool, global etc/di.xml: Model\SmsMethod, roles Verification and Enforcement. |
| Storefront route | Added to frontName customersignin before Hryvinskyi_CustomerSignInFrontend: sms/setup (GET), sms/sendsetup (POST, phone), sms/confirmsetup (POST, code), sms/send (POST, factor_id), sms/verify (POST, factor_id and code). Every POST carries the form key. |
| Layout | customersignin_sms_setup (the setup page, cacheable="false"); customer.signin.security.sms in customer.signin.security.sections; customer.signin.verify.sms in customer.signin.verify.methods. |
| Table | hryvinskyi_customer_sign_in_sms: the number of each phone factor in E.164, deleted with its factor. |
| Verification codes | Purposes customer_sign_in_sms_setup (adding a phone) and customer_sign_in_sms (the second step), 6 digits, 5 entries. |
| Rate-limit buckets | customer_sign_in_sms_customer (5 per hour) and customer_sign_in_sms_client (20 per hour), sliding windows. |
The package registers no cron job, command or ACL resource.
Extension points
The method joins the engine's pool from the global etc/di.xml, keyed by its code. An area di.xml that names this argument would replace the list, so a method is never registered there.
<type name="Hryvinskyi\CustomerSignIn\Model\MethodPool"> <arguments> <argument name="methods" xsi:type="array"> <item name="sms" xsi:type="object">Hryvinskyi\CustomerSignInSms\Model\SmsMethod</item> </argument> </arguments> </type>
SmsMethod implements Hryvinskyi\CustomerSignInApi\Api\SignInMethodInterface: code sms, label Text message, roles [SignInRole::Verification, SignInRole::Enforcement], switched on per website by customer_sign_in/sms/enabled. The text goes through Hryvinskyi\Sms\Api\SmsSenderInterface, so a store adds its SMS provider as a transport of hryvinskyi/magento2-sms, never in this package.
Tests
Run from the Magento root.
- Unit:
vendor/bin/phpunit -c app/code/Hryvinskyi/CustomerSignInSms/phpunit.xml.dist. - 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/CustomerSignInSms/Test/Integration. - Static analysis: PHPStan 2.x at level 10 with the package's
phpstan.neon.dist(phpstan analyse -c app/code/Hryvinskyi/CustomerSignInSms/phpstan.neon.dist --level=10) andvendor/bin/phpcs --standard=app/code/Hryvinskyi/CustomerSignInSms/phpcs.xml.dist app/code/Hryvinskyi/CustomerSignInSms.
Translations
The storefront wording, the text message and the admin labels ship 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.