承接 yossivic/otp 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

yossivic/otp

最新稳定版本:v1.3.2

Composer 安装命令:

composer require yossivic/otp

包简介

A simple package for generating and validating OTP (One Time Password) for Laravel

README 文档

README

A lightweight and flexible Laravel package for generating and validating one-time passwords (OTP).
Supports storage drivers (cache and database), secure hashing, configurable expiry, and customizable OTP format.

Features

  • Secure hashing (no plain OTP stored)
  • Supports cache and database storage drivers
  • Configurable OTP length and type (numeric / alphanumeric / hex)
  • Expiry control (minutes or seconds)
  • Pluggable repository pattern (swap storage without changing code)
  • Returns structured validation response (['valid' => bool, 'message' => string])
  • Optional Facade: Otp::generate() / Otp::validate()
  • Publishable config and (optional) migrations

Installation

Install via Composer:

composer require yossivic/otp

Publish Configuration

php artisan vendor:publish --tag=otp-config

This copies the package config to:

config/otp.php

Database Storage Setup (optional)

If you want persistent OTP storage (database) instead of cache (is default value):

  1. Publish migrations (if you included them)
php artisan vendor:publish --tag=otp-migrations

This should copy migration file(s) into:

database/migrations/2025_xx_xx_xxxxxx_create_otps_table.php

2.Migrate

php artisan migrate

Usage

You can use the service either by resolving from the container (dependency injection / app()) or via the facade.

The service returns OTP (string) on generation and a structured array on validation:

[
  'valid' => true|false,
  'message' => '...'
]
  1. Using Dependency Injection (recommended)
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Yossivic\Otp\Services\OtpService;

class OtpController extends Controller
{
    protected OtpService $otpService;

    public function __construct(OtpService $otpService)
    {
        $this->otpService = $otpService;
    }

    public function generate(Request $request)
    {
        $identifier = $request->input('identifier'); // e.g. user id, email, phone
        $otp = $this->otpService->generate($identifier);

        // Send OTP to user via your chosen channel (outside this package)
        return response()->json(['otp' => $otp]);
    }

    public function validate(Request $request)
    {
        $identifier = $request->input('identifier');
        $code = $request->input('otp');

        $result = $this->otpService->validate($identifier, $code);

        if ($result['valid']) {
            return response()->json(['message' => 'OTP valid']);
        }

        return response()->json(['message' => $result['message']], 422);
    }
}

Manual resolution (anywhere in code):

$otpService = app(\Yossivic\Otp\Services\OtpService::class);

$otp = $otpService->generate('user_123');           // returns generated OTP (string)
$result = $otpService->validate('user_123', '123456'); // returns ['valid'=>..., 'message'=>...]
  1. Using the Facade (shortcut)
use Otp;

$otp = Otp::generate('user_123');

$result = Otp::validate('user_123', '123456');

if ($result['valid']) {
    // success
} else {
    // $result['message'] explains the failure (expired, not found, max attempts, invalid)
}

Repository switching (cache vs db)

Switch storage by editing

config/otp.php:

// cache (fast, recommended)
'repository' => 'cache',

// or for persistent storage
'repository' => 'db',

Contributing

I am not perfect. please open an issue to discuss major features or you have any suggestion to improve.

统计信息

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

GitHub 信息

  • Stars: 6
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-11-23