{"id":25852745,"url":"https://github.com/alexeygeno/phone-verification-php","last_synced_at":"2025-03-01T14:28:56.584Z","repository":{"id":154633929,"uuid":"615011565","full_name":"alexeygeno/phone-verification-php","owner":"alexeygeno","description":"An extensible and configurable php library for phone verification","archived":false,"fork":false,"pushed_at":"2023-09-20T15:17:31.000Z","size":142,"stargazers_count":6,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-18T04:06:34.977Z","etag":null,"topics":["2fa","message-bird","mongodb","otp","otp-verification","phone","php","redis","sms","twilio","verification","verify","vonage"],"latest_commit_sha":null,"homepage":"https://packagist.org/packages/alexgeno/phone-verification","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/alexeygeno.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2023-03-16T19:12:50.000Z","updated_at":"2024-08-02T12:20:39.000Z","dependencies_parsed_at":null,"dependency_job_id":"389fd564-5701-4820-9f9a-38876ed2bc8d","html_url":"https://github.com/alexeygeno/phone-verification-php","commit_stats":{"total_commits":68,"total_committers":1,"mean_commits":68.0,"dds":0.0,"last_synced_commit":"3dfce1ba076f0a0c4a598a09839f5d446e12126c"},"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexeygeno%2Fphone-verification-php","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexeygeno%2Fphone-verification-php/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexeygeno%2Fphone-verification-php/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexeygeno%2Fphone-verification-php/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alexeygeno","download_url":"https://codeload.github.com/alexeygeno/phone-verification-php/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241377391,"owners_count":19953088,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["2fa","message-bird","mongodb","otp","otp-verification","phone","php","redis","sms","twilio","verification","verify","vonage"],"created_at":"2025-03-01T14:28:56.018Z","updated_at":"2025-03-01T14:28:56.574Z","avatar_url":"https://github.com/alexeygeno.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Phone Verification\n\n[![Build Status](https://github.com/alexeygeno/phone-verification-php/workflows/Tests/badge.svg)](https://github.com/alexeygeno/phone-verification-php/actions/workflows/tests.yml)\n[![Build Status](https://github.com/alexeygeno/phone-verification-php/workflows/PHPStan/badge.svg)](https://github.com/alexeygeno/phone-verification-php/actions/workflows/phpstan.yml)\n[![Build Status](https://github.com/alexeygeno/phone-verification-php/workflows/CodeSniffer/badge.svg)](https://github.com/alexeygeno/phone-verification-php/actions/workflows/cs.yml)\n[![Coverage Status](https://coveralls.io/repos/github/alexeygeno/phone-verification-php/badge.svg)](https://coveralls.io/github/alexeygeno/phone-verification-php)\n\nSigning in or signing up on a modern website or mobile app typically follows these steps:\n- A user initiates verification by submitting a phone number\n- The user receives an SMS or a call with a one-time password [(OTP)](https://en.wikipedia.org/wiki/One-time_password)\n- The user completes verification by submitting the [OTP](https://en.wikipedia.org/wiki/One-time_password)\n\nThis extensible and configurable library allows to set this up just with a few lines of code\n## Requirements\n- Supported PHP versions: 7.4, 8.0, 8.1, 8.2\n- [ Composer](https://getcomposer.org/)\n- Any of the supported sender SDKs: [twilio/sdk](https://github.com/twilio/twilio-php), [messagebird/php-rest-api](https://github.com/messagebird/php-rest-api), [vonage/client](https://github.com/vonage/vonage-php-sdk-core)\n- Any of the supported storage clients: [predis/predis](https://github.com/predis/predis), [mongodb/mongo-php-library](https://github.com/mongodb/mongo-php-library)\n\n\n## Installation\n```shell\ncomposer require alexgeno/phone-verification\n```\n**Note:** All supported storage clients and sender SDKs are in **require-dev** section. In a production environment you must manually install only what you use.\n## Basic Usage\n\n### Instantiation\n[ Predis](https://github.com/predis/predis) as a **storage** and  [ Twilio](https://github.com/twilio/twilio-php) as a **sender** are used for the demonstration\n```php\nuse AlexGeno\\PhoneVerification\\Storage\\Redis;\nuse AlexGeno\\PhoneVerification\\Sender\\Twilio;\nuse AlexGeno\\PhoneVerification\\Manager;\n\n$storage = new Redis(new \\Predis\\Client('tcp://127.0.0.1:6379'));\n$sender = new Twilio(new \\Twilio\\Rest\\Client('ACXXXXXX', 'YYYYYY'), ['from' =\u003e '+442077206312']);\n$manager = new Manager($storage);\n```\n### There are two stages in the verification process\n\n**Initiation** -  a **storage** and a **sender** are required for this stage. A user submits a phone and as a result receives an [otp](https://en.wikipedia.org/wiki/One-time_password)\n\n```php\n$manager-\u003esender($sender)-\u003einitiate('+15417543010');\n```\n**Completion** - only a storage is required for this stage. The user submits the [ otp](https://en.wikipedia.org/wiki/One-time_password) to verify the phone\n```php\n$manager-\u003ecomplete('+15417543010', 1234);\n```\nThat's basically it. More advanced usage including **otp length customization**, **rate limiters**, **messages customization** you can derive from the following sections.\n\n## Demo\n**Initiation** \n```shell\nphp example/initiate.php --storage redis --sender messageBird --to +15417543010\n```\n**Completion** \n```shell\nphp example/complete.php --storage redis --to +15417543010 --otp 1111\n```\n**Note**: See [DEVELOPMENT.md](DEVELOPMENT.md) as an option for how to set up a development environment\n\n## Extending\nTo add a new **sender** just create a new class\n```php\nnamespace AlexGeno\\PhoneVerification\\Sender;\n\nclass Plivo implements I\n{ \n    //...\n}\n```\nTo add a new **storage** just create a new class\n```php\nnamespace AlexGeno\\PhoneVerification\\Storage;\n\nclass DynamoDb implements I\n{ \n    //...\n}\n```\n## Advanced usage\nRate limit params and otp params might be customized\n\n**Initiation**\n```php\nuse AlexGeno\\PhoneVerification\\Storage\\Redis;\nuse AlexGeno\\PhoneVerification\\Sender\\Twilio;\nuse AlexGeno\\PhoneVerification\\Manager;\nuse AlexGeno\\PhoneVerification\\Exception\\RateLimit;\n\n$config = [\n    'rate_limits' =\u003e [\n        'initiate' =\u003e [\n            'period_secs' =\u003e 86400,\n            'count' =\u003e 10,\n            'message' =\u003e\n                fn($phone, $periodSecs, $count) =\u003e\n                    sprintf('You can send only %d sms in %d hours.', $count, $periodSecs / 60 / 60)\n        ]\n    ],\n    'otp' =\u003e [\n        'length' =\u003e 4, // 1000..9999\n        'message' =\u003e  fn($otp) =\u003e sprintf('Your code is %d', $otp) // The text a user receives\n    ]\n];\n\n$storage = new Redis(new \\Predis\\Client('tcp://127.0.0.1:6379'));\n$sender = new Twilio(new \\Twilio\\Rest\\Client('ACXXXXXX', 'YYYYYY'), ['from' =\u003e '+442077206312']);\n\ntry {\n    (new Manager($storage, $config))-\u003esender($sender)-\u003einitiate('+15417543010');\n} catch (RateLimit $e) {\n    echo $e-\u003egetMessage(); // 'You can send only 10 sms in 24 hours'\n}\n```\n\n**Completion**\n```php\nuse AlexGeno\\PhoneVerification\\Storage\\Redis;\nuse AlexGeno\\PhoneVerification\\Manager;\nuse AlexGeno\\PhoneVerification\\Exception\\RateLimit;\nuse AlexGeno\\PhoneVerification\\Exception\\Otp;\n\n$config = [\n    'rate_limits' =\u003e [\n        'complete' =\u003e [\n            'period_secs' =\u003e 300,\n            'count' =\u003e 5,\n            'message' =\u003e\n                fn($phone, $periodSecs, $count) =\u003e\n                    sprintf('You are trying to use an incorrect code %d times in %d minutes', $count, $periodSecs / 60)\n        ]\n    ],\n    'otp' =\u003e [\n        'message_expired' =\u003e\n            fn($periodSecs, $otp) =\u003e\n                sprintf('Code is expired. You have only %d minutes to use it.', $periodSecs / 60),\n        'message_incorrect' =\u003e  fn($otp) =\u003e 'Code is incorrect'\n    ]\n];\n$storage = new Redis(new \\Predis\\Client('tcp://127.0.0.1:6379'));\n\ntry {\n    (new Manager($storage, $config))-\u003ecomplete('+15417543010', 1234);\n} catch (RateLimit | Otp $e) {\n    // 'Code is incorrect' ||\n    // 'Code is expired. You have only 5 minutes to use it.' ||\n    // 'You are trying to use an incorrect code 5 times in 5 minutes'\n    echo $e-\u003egetMessage();\n}\n```\n**Note:** Of course,  you can define all **$config** options and instantiate all classes at the same place in your code.\nIt is split here just to make it more clear what belongs to **the initiation stage** and what to **the completion stage**\n\u003cbr /\u003e**Note:** Each **$config** option has a default value. You should redefine only what you need.\n\n### MongoDb indexes\nIf you use MongoDb as a **storage** you may have noticed that the expiration functionality is based on indexes.\nThey can be created automatically. It's recommended though to use this option only in a non-production environment. It's disabled by default.\n\n```php\nuse AlexGeno\\PhoneVerification\\Storage\\MongoDb;\n\n$storage = new MongoDb(new \\MongoDB\\Client('mongodb://127.0.0.1:27017'), ['indexes'=\u003e true]);\n```\n## Contributing\nSee [CONTRIBUTING.md](CONTRIBUTING.md)\n\n## Development\nSee [DEVELOPMENT.md](DEVELOPMENT.md) as an option for how to set up a development environment \n\n## Licence\nThe code for **Phone Verification** is distributed under the terms of the [MIT](LICENSE.txt) license.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexeygeno%2Fphone-verification-php","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falexeygeno%2Fphone-verification-php","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexeygeno%2Fphone-verification-php/lists"}