ez-php/auth 问题修复 & 功能扩展

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

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

ez-php/auth

最新稳定版本:1.11.1

Composer 安装命令:

composer require ez-php/auth

包简介

Authentication module for the ez-php framework — session and token-based auth with a flexible user provider interface

README 文档

README

Authentication module for the ez-php framework — session, Bearer token, JWT, and personal access token authentication with a flexible user provider interface.

CI

Requirements

  • PHP 8.5+
  • ez-php/framework 0.*

Installation

composer require ez-php/auth

Setup

Register the service provider in your application:

$app->register(\EzPhp\Auth\AuthServiceProvider::class);

// Optional — register JWT support:
$app->register(\EzPhp\Auth\JwtServiceProvider::class);

Implement UserProviderInterface to connect your user storage:

use EzPhp\Auth\UserProviderInterface;

class UserProvider implements UserProviderInterface
{
    public function findById(int|string $id): ?UserInterface { ... }
    public function findByToken(string $token): ?UserInterface { ... }
}

Bind it before AuthServiceProvider:

$this->app->bind(UserProviderInterface::class, UserProvider::class);

Usage

Session / Bearer token authentication

use EzPhp\Auth\Auth;

// Authenticate
Auth::login($user);
$user = Auth::user();
Auth::logout();

// Protect routes with middleware
$router->get('/dashboard', $handler)->middleware(\EzPhp\Auth\Middleware\AuthMiddleware::class);

JWT authentication

JWT_SECRET=your-secret-key
JWT_TTL=3600
$jwt = $app->make(\EzPhp\Auth\Jwt\JwtManager::class);

$token    = $jwt->issue($user->getAuthId());
$claims   = $jwt->validate($token);

// Protect routes
$router->get('/api/me', $handler)->middleware(\EzPhp\Auth\Middleware\JwtMiddleware::class);

Personal access tokens

$manager = $app->make(\EzPhp\Auth\PersonalAccessTokenManager::class);

[$rawToken, $token] = $manager->create($userId, 'my-token', ['read', 'write']);
$token = $manager->find($rawToken);
$manager->revoke($token->id);

Register the bundled migration before migrating:

database/migrations/2024_01_01_000000_create_personal_access_tokens_table.php

Console command

# Generate a personal access token for a user
php ez auth:token <user_id> <name> [--abilities=read,write] [--expires=3600]

Classes

Class Description
Auth Static façade — login(), logout(), user(), check(), id(), hashPassword(), verifyPassword()
AuthServiceProvider Registers Auth singleton; optionally injects UserProviderInterface
UserInterface Contract for authenticated user objects — getAuthId()
UserProviderInterface Contract for user lookup — findById(), findByToken()
AuthorizableInterface Optional contract for authorization checks on user objects
PersonalAccessToken Immutable value object — isExpired(), can()
PersonalAccessTokenManager Token CRUD — create(), find(), revoke(), rotate(), pruneExpired()
AuthMiddleware Bearer token middleware (static list or provider mode)
JwtMiddleware JWT Bearer token middleware with optional blacklist and user resolution
JwtManager Issues and validates HMAC-HS256 JWTs
JwtBlacklist Cache-backed token blacklist (SHA-256 keyed)
JwtServiceProvider Registers JwtManager and JwtBlacklist
Console\TokenCommand auth:token CLI command

License

MIT — Andreas Uretschnig

统计信息

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

GitHub 信息

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

其他信息

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

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固