定制 alhaji-aki/laravel-phone-number-verification 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

alhaji-aki/laravel-phone-number-verification

Composer 安装命令:

composer require alhaji-aki/laravel-phone-number-verification

包简介

A package to help verify phone numbers using otp tokens

README 文档

README

This is a simple package to help verify users phone number. This package uses alhaji-aki/laravel-otp-token to generate and send otp tokens to users

Installation

You can install the package via composer by running:

composer require "alhaji-aki/laravel-phone-number-verification"

After the installation has completed, the package will automatically register itself. Run the following to publish the migration, config and lang file

php artisan phone-number-verification:install

This publishes a PhoneNumberVerificationController in App\Http\Controllers\Auth.

NOTE: This package does not work on views so you will have to do some minor changes if you want to display views instead json responses.

Since this package uses alhaji-aki/laravel-otp-token, you will have to publish its files

php artisan vendor:publish --provider="AlhajiAki\OtpToken\OtpTokenServiceProvider"

After publishing the files, you can run migrations:

php artisan migrate

Let your model implement the CanSendOtpToken contract and use the CanSendOtpToken trait like so:

<?php

namespace App\Models;

use AlhajiAki\OtpToken\Contracts\CanSendOtpToken as CanSendOtpTokenContract;
use AlhajiAki\OtpToken\Traits\CanSendOtpToken;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable implements CanSendOtpTokenContract
{
    use Notifiable, CanSendOtpToken;
}

To learn more about this check out alhaji-aki/laravel-otp-token.

Now let your model implement MustVerifyPhoneNumber contract and MustVerifyPhoneNumber trait to be able to allow users verify their phone numbers. Like this:

<?php

namespace App\Models;

use AlhajiAki\OtpToken\Contracts\CanSendOtpToken as CanSendOtpTokenContract;
use AlhajiAki\OtpToken\Traits\CanSendOtpToken;
use AlhajiAki\PhoneNumberVerification\Contracts\MustVerifyPhoneNumber as MustVerifyPhoneNumberContract;
use AlhajiAki\PhoneNumberVerification\Traits\MustVerifyPhoneNumber;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable implements CanSendOtpTokenContract, MustVerifyPhoneNumberContract
{
    use Notifiable, CanSendOtpToken, MustVerifyPhoneNumber;
}

For this to work properly, you will have to override three methods in your User model. There are:

  • phoneNumberAttribute(): This is the attribute that represents the phone number column in your database.
  • phoneNumberVerificationAttribute(): This is the timestamp attribute that will be updated to indicate that a user is verified.
  • sendPhoneNumberVerificationNotification(): This is where the notification will be sent. This receives the token to be sent.

An example implementation is:

<?php

namespace App\Models;

use AlhajiAki\OtpToken\Contracts\CanSendOtpToken as CanSendOtpTokenContract;
use AlhajiAki\OtpToken\Traits\CanSendOtpToken;
use AlhajiAki\PhoneNumberVerification\Contracts\MustVerifyPhoneNumber as MustVerifyPhoneNumberContract;
use AlhajiAki\PhoneNumberVerification\Traits\MustVerifyPhoneNumber;
use App\Notifications\Auth\VerifyPhoneNumber;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable implements CanSendOtpTokenContract, MustVerifyPhoneNumberContract
{
    use Notifiable, CanSendOtpToken, MustVerifyPhoneNumber;

    public function phoneNumberAttribute(): string
    {
        return 'phone_number';
    }

    public function phoneNumberVerificationAttribute(): string
    {
        return 'phone_number_verified_at';
    }

    public function sendPhoneNumberVerificationNotification(string $token): void
    {
        $this->notify(new VerifyPhoneNumber($token));
    }
}

If you want users to be notified when they register on your application add SendPhoneNumberVerificationNotification listener to listeners of the Registered event in the EventServiceProvider. Like this:

/**
 * The event listener mappings for the application.
 *
 * @var array<class-string, array<int, class-string>>
 */
protected $listen = [
    ...
    Registered::class => [
        SendPhoneNumberVerificationNotification::class,
    ],
    ...
];

Dont forget to import the full namespace use \AlhajiAki\PhoneNumberVerification\Listeners\SendPhoneNumberVerificationNotification;

Finally, you will have to register the \AlhajiAki\PhoneNumberVerification\Middleware\EnsurePhoneNumberIsVerified::class middleware in your Http Kernel. Example:

protected $routeMiddleware = [
    ...
    'mobile-verified' => \AlhajiAki\PhoneNumberVerification\Middleware\EnsurePhoneNumberIsVerified::class,
    ...
];

Then you can protect your routes with that middleware.

Testing

composer test

Formatting

composer format

Static analysis

composer analyse

Contributing

Please see CONTRIBUTING for details.

License

The MIT License (MIT). Please see License File for more information.

alhaji-aki/laravel-phone-number-verification 适用场景与选型建议

alhaji-aki/laravel-phone-number-verification 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9.56k 次下载、GitHub Stars 达 7, 最近一次更新时间为 2020 年 12 月 12 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 alhaji-aki/laravel-phone-number-verification 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 alhaji-aki/laravel-phone-number-verification 我们能提供哪些服务?
定制开发 / 二次开发

基于 alhaji-aki/laravel-phone-number-verification 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 9.56k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 7
  • 点击次数: 36
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 7
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-12-12