ronasit/laravel-clerk
Composer 安装命令:
composer require ronasit/laravel-clerk
包简介
Package provides auth guard to auth user via the Clerk
README 文档
README
Laravel Clerk Guard
Introduction
This package offers an authentication guard to seamlessly integrate Clerk authentication into your Laravel project.
Installation
- Use Composer to install the package:
composer require ronasit/laravel-clerk
- Run package's
installcommand
php artisan laravel-clerk:install
- Populate the necessary configuration options in
config/clerk.php.
Configuration
Set the following environment variables to configure the package:
CLERK_ALLOWED_ISSUER— the expectedissclaim value of incoming JWT tokens.CLERK_ALLOWED_ORIGINS— comma-separated list of allowedazpclaim values.CLERK_SECRET_KEY— your Clerk API secret key, used to verify the token signature.CLERK_SIGNER_KEY— PEM content of the public JWT key as a string. Takes priority overCLERK_SIGNER_KEY_PATHwhen set.CLERK_SIGNER_KEY_PATH— path to the public JWT key file, relative tobase_path(). Defaults toclerk.pem. Used as a fallback whenCLERK_SIGNER_KEYis not set.
You can find the public JWT key in your Clerk dashboard under "Configure" → "API keys" → "JWKS Public Key".
Usage
By default, your app returns the User class with just the external_id property, which holds the user's ID in Clerk.
To customize this behavior, you'll need to create your own UserRepository that implements the UserRepositoryContract.
Then, rebind it in one of the service providers:
use RonasIT\Clerk\Contracts\UserRepositoryContract; use App\Support\Clerk\MyAwesomeUserRepository; class AppServiceProvider extends ServiceProvider { public function boot(): void { $this->app->bind(UserRepositoryContract::class, MyAwesomeUserRepository::class); } }
Testing
To test authenticated user requests guarded by ClerkGuard, use the TokenMockTrait:
- Ensure clerk config is filled using
.env.testingfile or dynamically. The signer key is set automatically:
Config::set('clerk', [ 'allowed_issuer' => 'issuer', 'secret_key' => 'my_secret_key', ]);
- Generate a JWT token and pass it to the
Authorizationheader:
use RonasIT\Clerk\Traits\TokenMockTrait; class UserRepositoryTest extends TestCase { use TokenMockTrait; public function test() { $clerkToken = $this ->createJWTToken( relatedTo: 'user_id', issuer: 'issuer', ) ->toString(); $this->withHeader('Authorization', "Bearer {$clerkToken}"); } }
- You may also pass custom claims to the token using
claimsparameter:
$clerkToken = $this ->createJWTToken( relatedTo: 'user_id', issuer: 'issuer', claims: [ 'email' => 'user@mail.com', 'phone' => '+1234567789', ], )->toString();
ronasit/laravel-clerk 适用场景与选型建议
ronasit/laravel-clerk 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10.56k 次下载、GitHub Stars 达 9, 最近一次更新时间为 2024 年 11 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 ronasit/laravel-clerk 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ronasit/laravel-clerk 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 10.56k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 9
- 点击次数: 19
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-11-04