kenad/laravel-authkit 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

kenad/laravel-authkit

Composer 安装命令:

composer require kenad/laravel-authkit

包简介

A professional Laravel API authentication package with Sanctum, device management, email verification, password reset and rate limiting.

README 文档

README

Laravel AuthKit Logo

Latest Version on Packagist GitHub Tests Action Status Total Downloads PHP from Packagist License

The Ultimate Laravel API Authentication Starter Kit 🚀

Laravel AuthKit is a powerful, zero-configuration API authentication package built on top of Laravel Sanctum. It instantly supercharges your Laravel application with enterprise-grade features while remaining incredibly easy to use.

Stop writing boilerplate auth code for every new project. Just install AuthKit, and within 2 minutes, your API is fully equipped.

✨ Features

  • 🔐 Complete Auth API: Register, Login, Logout, Forgot Password, Reset Password.
  • 📱 Device Management: Track logins by device, let users view active sessions and revoke them remotely.
  • 👮‍♂️ Roles & Permissions: Built-in, lightweight module to manage user authorization.
  • 🏢 Teams (Multi-tenancy): Native support for users belonging to multiple teams.
  • 🔑 Token Abilities: Granular control over Sanctum token scopes right from the login request.
  • 🛡️ Rate Limiting: Intelligent throttling to prevent brute-force attacks.
  • ✉️ Email Verification: Seamless flow mapped to Laravel's native events.
  • 📝 Audit Logging: Traceable logins and logouts for security compliance.
  • 🎨 Consistent API Responses: Standardized JSON format across all auth endpoints.

📦 Installation

REQUIREMENT: PHP 8.2+ and Laravel 10/11/12+.

You can install the package via composer:

composer require kenad/laravel-authkit

Publish the package configuration and migrations:

php artisan vendor:publish --provider="Kenad\AuthKit\AuthKitServiceProvider"

Run the migrations:

php artisan migrate

⚙️ Configuration

AuthKit is deeply customizable. In config/authkit.php, you can adjust token expiration, rate limiting rules, enable/disable modules (like Device Management and Audit Logging), and set your custom User model.

return [
    'token_expiration' => 60 * 24 * 7, // 7 days
    
    'rate_limit' => [
        'max_attempts' => 5,
        'decay_minutes' => 1,
    ],
    
    'device_management' => true,
    'email_verification' => true,
    'audit_log' => true,
];

👩‍💻 Usage

AuthKit handles the API routing automatically (prefixed with /api/auth/ by default).

1️⃣ Authentication API

Simply hit these plug-and-play endpoints:

  • POST /api/auth/register: { name, email, password, password_confirmation }
  • POST /api/auth/login: { email, password, device_name, platform, abilities }
  • POST /api/auth/logout: (Requires Bearer Token)
  • POST /api/auth/logout-all: Logout from all devices.
  • GET /api/auth/me: Get current user info.

Standardized Response Format:

{
  "success": true,
  "message": "Login successful.",
  "data": {
    "user": { "id": 1, "name": "kenad", "email": "kenad@example.com" },
    "access_token": "1|abcdef123456",
    "token_type": "Bearer",
    "expires_in": 10080
  }
}

2️⃣ Roles and Permissions

Add the HasAuthKitRoles trait to your User model:

use Kenad\AuthKit\Traits\HasAuthKitRoles;

class User extends Authenticatable
{
    use HasAuthKitRoles;
}

Now you can intuitively assign roles and check permissions:

$user->assignRole('admin');
$user->assignRole('editor');

$role = Role::create(['name' => 'writer']);
$role->givePermissionTo('publish articles');

$user->hasRole('admin'); // true
$user->hasPermissionTo('publish articles'); // true

Middleware included! Protect your routes easily:

Route::get('/admin', [AdminController::class, 'index'])->middleware('authkit.role:admin');

3️⃣ Teams (Multi-Tenancy)

Building a SaaS? Use the HasAuthKitTeams trait on your User model:

use Kenad\AuthKit\Traits\HasAuthKitTeams;

class User extends Authenticatable
{
    use HasAuthKitTeams;
}

Manage teams fluently:

$team = Team::create(['name' => 'Acme Corp', 'owner_id' => $user->id]);

$user->belongsToTeam($team); // true
$user->ownsTeam($team); // true
$user->switchTeam($team); // Set active context

4️⃣ Device Management API

If enabled in the config, users can manage their active login sessions:

  • GET /api/auth/devices: List all active devices.
  • DELETE /api/auth/devices/{id}: Revoke access for a specific device.

5️⃣ Facade Magic

Prefer writing custom controllers? AuthKit exposes a beautiful Facade that handles the complex business logic for you:

use Kenad\AuthKit\Facades\AuthKit;
use Kenad\AuthKit\DTOs\LoginData;

// Register explicitly
$user = AuthKit::register(new RegisterData('John', 'john@example.com', 'secret'));

// Login cleanly
$result = AuthKit::login(new LoginData('john@example.com', 'secret'), 'iPhone 15');
return response()->json(['token' => $result['token']]);

🧪 Testing

AuthKit is built with Pest PHP and is highly decoupled into Actions and Contracts for supreme testability.

composer test

🏗️ Architecture

Under the hood, AuthKit uses a highly scalable modular architecture inspired by Spatie:

  • Actions pattern for atomic, single-responsibility business logic (LoginUser, ResetPassword).
  • DTOs (Data Transfer Objects) for strong typing between the HTTP layer and Application layer.
  • Contracts/Services for swappable implementations. Let's say you want to change how tokens are generated? Just implement TokenServiceInterface and re-bind it in your AppServiceProvider!

🤝 Contributing

Please see CONTRIBUTING for details.

🔐 Security Vulnerabilities

If you discover any security-related issues, please email mohamedkenad10@example.com instead of using the issue tracker.

📄 License

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

kenad/laravel-authkit 适用场景与选型建议

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-03-09