farzai / phone-verification
1.0.0
2022-01-24 17:39 UTC
Requires
- php: ^7.4|^8.0
- ext-json: *
Requires (Dev)
- guzzlehttp/guzzle: ^7.0
- phpunit/phpunit: ^9.4
This package is auto-updated.
Last update: 2024-09-29 05:50:06 UTC
README
这是一个通过短信OTP进行用户验证的系统
例如,在您的系统中存在会员系统,并且需要通过短信OTP验证用户的情况下,您可以使用这个库来帮助您,以使您的系统更加完善,并且可以轻松管理
简要的使用示例
use Farzai\PhoneVerification\Adapters\SmsAdapter; use Farzai\PhoneVerification\SMS\Providers\NexmoProvider; use Farzai\PhoneVerification\Verifier; use Farzai\PhoneVerification\Exceptions\InvalidVerificationException; use Farzai\PhoneVerification\Exceptions\VerificationHasExpired; // การใช้งาน // เริ่มที่ตั้งต่าการใช้งาน $adapter = new SmsAdapter(new NexmoProvider(), $repository); // สร้างการยืนยัน และส่ง SMS-OTP ไปยังเบอร์ปลายทาง $verifier = new Verifier($adapter); $entity = $verifier->create( $phoneNumber = "0988887777" ); // ผลลัพท์ที่ได้ ท่านสามารถนำมาใช้งานต่อได้ $entity->phone_number; $entity->reference; $entity->code; // --------------------------- // การยืนยัน OTP $phoneNumber = "0988887777"; $reference = "xxxxxx"; $code = "xxxxxx"; try { $entity = $verifier->verify($phoneNumber, $reference, $code); // ผลลัพท์ที่ได้ ท่านสามารถนำมาใช้งานต่อได้ $entity->phone_number; $entity->reference; $entity->code; // .... } catch (InvalidVerificationException) { // เกิดข้อผิดพลาด กรณีรหัสตรวสสอบไม่ถูกต้อง } catch (VerificationHasExpired) { // เกิดข้อผิดพลาด กรณีรหัสตรวจสอบหมดอายุแล้ว }
系统需求
{ "php": "^7.4|^8.0", "ext-json": "*" }
使用方法
通过Composer安装
composer require farzai/phone-verification
开始配置
由于使用过程中会保存数据,并从您的数据库中检索数据
因此,您必须告诉我们您如何保存数据以及如何检索数据,通过创建存储库和implements
接口,例如下面的示例
use Farzai\PhoneVerification\Repositories\VerificationRepository; use Farzai\PhoneVerification\Entities\Verification; class MyRepository implements VerificationRepository { /** * เมื่อระบบต้องการสร้างการยินยัน * * @param string $phoneNumber * @return Verification */ public function create(string $phoneNumber): Verification { // บันทึกลงฐานข้อมูลของท่าน ตรงนี้.... // ----- // จากนั้นสร้าง Entity และคืนค่า Entity ให้เรา return new Verification([ 'phone_number' => $phoneNumber, 'reference' => (string)random_int(10000, 99999), 'code' => (string)random_int(1000, 9999), ]); } /** * เมื่อระบบต้องการค้นหาเพื่อยืนยันว่ารหัสที่ส่งมาถูกหรือไม่? * * @param string $phoneNumber * @param string $reference * @param string $code * @return Verification|null */ public function findForValidate(string $phoneNumber, string $reference, string $code): ?Verification { // ค้นหาจากฐานข้อมูลของท่าน // .... // ถ้าไม่พบ ให้ return null ให้เรา // .... // return null; // ถ้าค้นพบให้ส่ง Entity กลับมาหาเรา return new Verification([ 'phone_number' => $phoneNumber, 'reference' => $reference, 'code' => $code, // (optional) // ใส่ parameter: expires_at ถ้าอยากให้เราตรวจสอบว่าหมดอายุหรือยัง 'expires_at' => new \DateTime('now'), ]) } /** * เมื่อระบบต้องการทำให้การยืนยันของ record นี้หมดอายุ * * @param Verification $verifier */ public function markAsExpired(Verification $verifier): Verification { // ทำให้ record นั้น "หมดอายุ" return $verifier; } /** * เมื่อระบบต้องการทำให้การยืนยันของ record นี้ถูกยืนยันไปแล้ว * * @param Verification $verifier */ public function markAsVerified(Verification $verifier): Verification { // ทำให้ record นั้น "ยืนยันแล้ว" return $verifier; } }
使用步骤
1. 发送SMS-OTP
use Farzai\PhoneVerification\Adapters\SmsAdapter; use Farzai\PhoneVerification\SMS\Providers\NexmoProvider; use Farzai\PhoneVerification\Verifier; use Farzai\PhoneVerification\Exceptions\InvalidVerificationException; use Farzai\PhoneVerification\Exceptions\VerificationHasExpired; // การใช้งาน // เริ่มที่ตั้งต่าการใช้งาน // ที่เก็บข้อมูลของเรา $repository = new MyRepository(); // ตัวส่ง SMS // ในตัวอย่างนี้ เราเลือกใช้ Nexmo เป็นตัวอย่างก่อน $sms = new NexmoProvider([ 'key' => 'xxxxxxxx', 'from' => 'xxxxxxxx', ]); // สร้าง Adapter $adapter = new SmsAdapter($sms, $repository); // สร้างตัวดำเนินการ $verifier = new Verifier($adapter); // ส่ง SMS-OTP $entity = $verifier->create( $phoneNumber = "0988887777" ); // ผลลัพท์ที่ได้ ท่านสามารถนำมาใช้งานต่อได้ $entity->phone_number; $entity->reference; $entity->code;
2. 验证和检查SMS-OTP
use Farzai\PhoneVerification\Adapters\SmsAdapter; use Farzai\PhoneVerification\SMS\Providers\NexmoProvider; use Farzai\PhoneVerification\Verifier; use Farzai\PhoneVerification\Exceptions\InvalidVerificationException; use Farzai\PhoneVerification\Exceptions\VerificationHasExpired; // รับ Input จากระบบของท่าน $phoneNumber = "0988887777"; $reference = "xxxxxx"; $code = "xxxxxx"; // ที่เก็บข้อมูลของเรา $repository = new MyRepository(); // สร้างตัวดำเนินการ $sms = new NexmoProvider([ 'key' => 'xxxxxxxx', 'from' => 'xxxxxxxx', ]); $adapter = new SmsAdapter($sms, $repository); $verifier = new Verifier($adapter); // ทำการตรวจสอบ try { $entity = $verifier->verify($phoneNumber, $reference, $code); // ผลลัพท์ที่ได้ ท่านสามารถนำมาใช้งานต่อได้ $entity->phone_number; $entity->reference; $entity->code; // .... } catch (InvalidVerificationException) { // เกิดข้อผิดพลาด กรณีรหัสตรวสสอบไม่ถูกต้อง } catch (VerificationHasExpired) { // เกิดข้อผิดพลาด กรณีรหัสตรวจสอบหมดอายุแล้ว }
示例
我们在这里附上了示例文件,以供参考 示例文件
许可
MIT许可(MIT)。有关更多信息,请参阅许可文件