abdukhaligov / laravel-otp

dev-master 2024-08-10 09:36 UTC

This package is auto-updated.

Last update: 2024-09-10 09:52:19 UTC


README

参考

Laravel OTP

简介

这是一个简单的包,用于生成和验证一次性密码(OTP)。这通常用于身份验证实现。

安装

通过 composer 安装

composer require abdukhaligov/laravel-otp

将服务提供者添加到 config/app.php 文件中

<?php
   /*
    |--------------------------------------------------------------------------
    | Autoloaded Service Providers
    |--------------------------------------------------------------------------
    |
    | The service providers listed here will be automatically loaded on the
    | request to your application. Feel free to add your own services to
    | this array to grant expanded functionality to your applications.
    |
    */

    'providers' => [
        ...
        Abdukhaligov\LaravelOtp\OtpServiceProvider::class,
    ];
...

将别名添加到 config/app.php 文件中

<?php

   /*
    |--------------------------------------------------------------------------
    | Class Aliases
    |--------------------------------------------------------------------------
    |
    | This array of class aliases will be registered when this application
    | is started. However, feel free to register as many as you wish as
    | the aliases are "lazy" loaded so they don't hinder performance.
    |
    */

    'aliases' => [
        ...
        'otp' => Abdukhaligov\LaravelOtp\OtpFacade::class,
    ];
...

运行迁移

php artisan migrate

使用方法

生成 OTP

<?php

Otp::generate(string $identifier, int $digits = 6, int $validity = 10)

Otp::validate(string $identifier, string $token)

删除过期的令牌

您可以通过运行以下 artisan 命令来删除过期的令牌

php artisan otp:clean

您也可以将此 artisan 命令添加到 app/Console/Kernel.php 中,以便在计划的时间内自动清理

<?php

protected function schedule(Schedule $schedule)
{
    $schedule->command('otp:clean')->daily();
}