roomies/phonable

从多个第三方提供商收集见解并验证电话号码。

0.5.0 2024-06-26 23:22 UTC

This package is auto-updated.

Last update: 2024-08-26 23:38:28 UTC


README

Latest Version on Packagist GitHub Tests Action Status Total Downloads

Roomies Phonable 为您的 Laravel 应用提供了一层抽象层,用于识别和验证电话号码。电话验证可用于帮助识别应用程序的合法用户,并作为处理双因素认证的一种方式。Phonable 为多个电话服务提供了实现,包括 Prelude(之前称为 Ding)、TwilioVonage

安装

您可以通过 Composer 安装此包

composer require roomies/phonable

您可以使用以下命令发布配置文件

php artisan vendor:publish --tag="phonable-config"

阅读配置文件以了解支持的服务并提供您首选服务的正确配置。

识别

识别是一种收集有关电话号码更多信息的方式,包括国家/地区、电话号码类型等。此功能由 Prelude 和 Vonage 支持。

// Return an instance of \Roomies\Phonable\Identification\IdentificationResult
$result = Identification::get('+12125550000');

或者,您可以传递一个实现了 Roomies\Phonable\Contracts\PhoneIdentifiable 的对象 - getIdentifiablePhoneNumber() 应返回 E.164 格式的电话号码。

use Roomies\Phonable\Facades\Identification;
use Roomies\Phonable\Contracts\PhoneIdentifiable;

class User implements PhoneIdentifiable
{
    /**
     * The identifiable phone number in E.164 format.
     */
    public function getIdentifiablePhoneNumber(): ?string
    {
        return '+12125550000';
    }
}

// Return an instance of \Roomies\Phonable\Identification\IdentificationResult
$result = Identification::get($user);

您可以根据需要随时更换驱动程序。

use Roomies\Phonable\Facades\Identification;

Identification::driver('prelude')->get($user);

验证

验证是一个两步过程,包括将生成的代码发送到电话号码,然后需要将其输入到您的应用程序中以完成过程。这确保您的用户可以及时访问提供的电话号码。此功能由 Prelude、Twilio 和 Vonage 支持。

// Return an instance of \Roomies\Phonable\Verification\VerificationRequest
$request = Verification::send('+12125550000');

// Return an instance of \Roomies\Phonable\Verification\VerificationResult
$result = Verification::verify($request->id, 'code');

或者,您可以传递一个实现了 Roomies\Phonable\Contracts\PhoneVerifiable 的对象 - getVerifiablePhoneNumber() 应返回 E.164 格式的电话号码,而 getVerifiableSession() 应返回先前存储的验证请求 ID。

use Roomies\Phonable\Facades\Verification;
use Roomies\Phonable\Contracts\PhoneIdentifiable;

class User implements PhoneVerifiable
{
    /**
     * The verifiable phone number in E.164 format.
     */
    public function getVerifiablePhoneNumber(): ?string
    {
        return '+12125550000';
    }

    /**
     * The current verification session identifier.
     */
    public function getVerifiableSession(): ?string
    {
        return $this->phone_verification_session;
    }
}

// Return an instance of \Roomies\Phonable\Verification\VerificationRequest
$request = Verification::send($user);

$user->update([
    'phone_verification_session' => $request->id,
]);

您需要存储验证会话 ID,因为它将用于完成过程。

当您从用户那里收到代码时,您可以调用带有提供的代码的 verify 方法。Roomies\Phonable\Verification\VerificationResult 是一个简单的枚举,表示结果。

use Roomies\Phonable\Verification\VerificationResult;

// Return an instance of \Roomies\Phonable\Verification\VerificationResult
$result = Verification::verify($user, 1234);

if ($result === VerificationResult::Successful) {
    $user->update([
        'phone_verified_at' => now(),
        'phone_verification_session' => null,
    ]);
}

您可以根据需要随时更换驱动程序。

use Roomies\Phonable\Facades\Verification;

Verification::driver('prelude')->send($user);

许可证

MIT 许可证(MIT)。有关更多信息,请参阅 许可证文件