定制 mydaniel/laravel-temporary-tokens 二次开发

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

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

mydaniel/laravel-temporary-tokens

Composer 安装命令:

composer require mydaniel/laravel-temporary-tokens

包简介

A flexible Laravel package to generate, manage, and validate temporary tokens, OTPs, or pins.

README 文档

README

Latest Version on Packagist Total Downloads License

A simple and flexible Laravel package that allows you to generate, manage, and validate temporary tokens, One-Time Passwords (OTPs), or PINs for tasks like authentication, email verification, password resets, and more.

Features

  • Generate numeric tokens of any length.
  • Set expiration dates for tokens.
  • Limit the number of times a token can be used.
  • Attach arbitrary data (metadata) to any token.
  • Associate tokens with any Eloquent model (e.g., User).
  • Includes an Artisan command to automatically prune expired tokens.
  • Fully configurable with no external dependencies.

Installation

You can install the package via Composer:

composer require mydaniel/laravel-temporary-tokens

Next, you should publish the configuration and migration files using the vendor:publish command:

php artisan vendor:publish --provider="MyDaniel\TemporaryTokens\TemporaryTokensServiceProvider"

Finally, run the migration to create the temporary_tokens table:

php artisan migrate

Usage

1. Preparing Your Model

First, add the HasTemporaryTokens trait to the model you wish to generate tokens for (e.g., your User model).

namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;
use MyDaniel\TemporaryTokens\Traits\HasTemporaryTokens;

class User extends Authenticatable
{
    use HasTemporaryTokens;

    // ...
}

2. Creating a Token

You can easily create new tokens using the fluent TokenBuilder.

Create a simple 6-digit token for a user:

$user = User::find(1);

// The `temporaryTokenBuilder` method comes from the HasTemporaryTokens trait
$temporaryToken = $user->temporaryTokenBuilder()->create();

echo $temporaryToken->token; // e.g., "123456"

Create a token with custom settings:

$user = User::find(1);

$temporaryToken = $user->temporaryTokenBuilder()
    ->setType('email-verification')       // Set a token type
    ->setTokenLength(5)                   // Generate a 5-digit token
    ->setUsageLimit(1)                    // Allow the token to be used only once
    ->setExpireDate(now()->addMinutes(15)) // Set a 15-minute expiration
    ->setMetadata(['source' => 'registration']) // Attach custom data
    ->create();

3. Finding and Validating a Token

To find and check the validity of a token, you can use the methods on the TemporaryToken model.

use MyDaniel\TemporaryTokens\Models\TemporaryToken;

$userToken = '123456';
$tokenType = 'email-verification';

// Find a token that is not expired and has not exceeded its usage limit
$token = TemporaryToken::findValid($userToken, $tokenType);

if ($token) {
    // The token is valid
    echo "Token is valid!";

    // You can also check if the token belongs to the correct user
    if ($token->tokenable_id === $user->id) {
        // ...
    }
}

The isValid() method is also available on a TemporaryToken instance to perform the same check:

if ($token && $token->isValid()) {
    // ...
}

4. Using a Token

After a token has been successfully used, you can increment its usage counter.

// Mark the token as "used"
$token->use();

// If the token reaches its usage limit, `hasExceededMaxUsage()` will return true
if ($token->hasExceededMaxUsage()) {
    echo "This token cannot be used anymore.";
}

Pruning Expired Tokens

You can remove expired tokens from your database by running the provided Artisan command.

php artisan temporary-tokens:prune-expired

By default, this command will prune tokens that expired more than 24 hours ago. To customize this, you can either use the --hours option or change the prune_expired_after_hours value in the config/temporary-tokens.php file.

# Prune tokens that expired more than 48 hours ago
php artisan temporary-tokens:prune-expired --hours=48

It is recommended to schedule this command to run daily in your app/Console/Kernel.php file.

// app/Console/Kernel.php
protected function schedule(Schedule $schedule)
{
    $schedule->command('temporary-tokens:prune-expired')->daily();
}

Changelog

Please see CHANGELOG.md for more information on what has changed recently.

Contributing

Contributions are welcome! Please feel free to fork the repository and submit a pull request.

Security Vulnerabilities

If you discover a security vulnerability within this package, please send an e-mail to Daniel Yousefi Far at daniyousefifar@gmail.com. All security vulnerabilities will be promptly addressed.

License

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

mydaniel/laravel-temporary-tokens 适用场景与选型建议

mydaniel/laravel-temporary-tokens 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 08 月 20 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「security」 「temporary」 「laravel」 「otp」 「token」 「pin」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

围绕 mydaniel/laravel-temporary-tokens 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-08-20