codemonster-ru/auth
最新稳定版本:v1.0.0
Composer 安装命令:
composer require codemonster-ru/auth
包简介
Framework-agnostic authentication primitives for Annabel applications.
README 文档
README
Framework-agnostic authentication primitives for Annabel applications.
Installation
composer require codemonster-ru/auth
Concepts
AuthenticatableInterfacedescribes a user object.UserProviderInterfaceretrieves and validates users.PasswordHasherInterfacehashes and verifies passwords.AuthorizerInterfacechecks abilities and policies.SessionGuardstores the authenticated user id incodemonster-ru/session.Authenticatemiddleware rejects guests with401or redirects them.Authorizemiddleware rejects requests that fail a gate ability check.
Example
use Codemonster\Auth\Guards\SessionGuard; use Codemonster\Auth\Hashing\NativePasswordHasher; use Codemonster\Auth\Providers\ArrayUserProvider; $hasher = new NativePasswordHasher(); $provider = new ArrayUserProvider([ new User(1, 'admin@example.com', $hasher->make('secret')), ], $hasher); $guard = new SessionGuard($provider, session()); if ($guard->attempt(['email' => 'admin@example.com', 'password' => 'secret'])) { echo $guard->id(); // 1 } $guard->logout(); // Invalidates the session by default.
ArrayUserProvider is intentionally small. Production applications can provide
their own database-backed implementation of UserProviderInterface.
Authorization
use Codemonster\Auth\Authorization\Gate; $gate = new Gate($guard); $gate->define('posts.update', fn($user, $post) => $user?->getAuthIdentifier() === $post->owner_id); if ($gate->allows('posts.update', $post)) { // ... }
In Annabel routes, the can middleware alias can read route parameters exposed
by the HTTP kernel:
router() ->get('/posts/{post}', [PostController::class, 'show']) ->middleware('can:posts.view,post');
Database users
DatabaseUserProvider retrieves users through
codemonster-ru/database's ConnectionInterface:
use Codemonster\Auth\Database\DatabaseUserProvider; $provider = new DatabaseUserProvider( db(), new NativePasswordHasher(), table: 'users', identifierColumn: 'id', passwordColumn: 'password', credentialKey: 'email', );
Annabel integration
codemonster-ru/annabel registers the auth services through
AuthServiceProvider. Applications can use auth() and user() helpers, and
protect routes with Codemonster\Auth\Middleware\Authenticate and
Codemonster\Auth\Middleware\Authorize.
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 3
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-06-10