rohsyl/laravel-otc
Composer 安装命令:
composer require rohsyl/laravel-otc
包简介
Laravel One Time Code Authentication
关键字:
README 文档
README
Laravel One Time Code Authentication allow you to send by mail an one time code to auth your users.
Installation
You can install the package via composer:
composer require rohsyl/laravel-otc
Run the installer
php artisan otc:install
Configuration
Edit config/otc.php
return [ 'notifier_class' => \Illuminate\Support\Facades\Notification::class, 'notification_class' => \rohsyl\LaravelOtc\Notifications\OneTimeCodeNotification::class, 'authenticatables' => [ 'user' => [ 'model' => \App\Models\User::class, 'identifier' => 'email', ] ] ];
notifier_class
Define what class will be called to send the notification. By default it use the Notification facade of Laravel.
'notifier_class' => \Illuminate\Support\Facades\Notification::class,
notification_class
Define what notification will be sent.
'notification_class' => \rohsyl\LaravelOtc\Notifications\OneTimeCodeNotification::class,
You can replace this class by any other notification, you will recieve a OtcToken $token as constructor parameters
public function __construct(OtcToken $token) { $this->token = $token; }
You can access the code that need to be sent from the $token variable
$token->code
authenticatables
This array will define a list of entites that can be used to get authentified. It's like a simplified version of laravel guard. I might move this to guard in the futur. The main goal is to set what model and what column are used to find the model in the database.
useris the name of the "guard"/typemodelis the corresponding eloquent modelidentifieris the identifier column that will be used to find the corresponding user
'user' => [ 'model' => \App\Models\User::class, 'identifier' => 'email', ]
Usage
Check
Check if the user is authenticated
Otc::check()
This method will return
trueorfalse.
If the user is not authentified you can return an error
if(!Otc::check()) { return Otc::unauthorizedResponse($user); }
This response will return 401 http error with the following body.
{
"request_code_url": ".../vendor/rohsyl/laravel-otc/auth/request-code",
"request_code_body": {
"type": "user",
"identifier": "test@test.com"
}
}
You must use the request_code_url as the url to request a code (ye seem obvious) and you must pass the request_code_body as the body in json format !
Request a code
Send a post request
POST /vendor/rohsyl/laravel-otc/auth/request-code
with body
{
"type": "user",
"identifier": "test@test.com"
}
You need to send the
typeand theidentifierof your authenticatables entity
An email will be sent to the corresponding entity if available. The email will contain the code.
Request a token
Send a post request
POST /vendor/rohsyl/laravel-otc/auth/code
with body
{
"type": "user",
"identifier": "test@test.com",
"code": <code>
}
You need to send the
codethat should have been retrieved from the user through a form or anything else.
You will recieve a token back
{
"token": "9vov6FjW47v6JjH...4iPzPH0PwpwdE"
}
And you can use this token for every further request.
Authentified request
When you have the token, you can send it with you request to be authentified.
Pass it in the headers
Authorization: Bearer <token>
Or in the query string
?token=<token>
Troubleshooting
CORS
If you use fruitcake/laravel-cors to manage CORS in your app. You will get CORS error when doing call to this package endpoints.
You will need to add a new path in your config/cors.php in the paths array
'paths' => [
// ...
'vendor/rohsyl/laravel-otc/*',
],
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please use the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
rohsyl/laravel-otc 适用场景与选型建议
rohsyl/laravel-otc 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.38k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2022 年 11 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「rohsyl」 「laravel-otc」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 rohsyl/laravel-otc 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 rohsyl/laravel-otc 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 rohsyl/laravel-otc 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Laravel One Time Code Authentication
Alfabank REST API integration
Laravel package for Accurate Online API integration.
Shared RCX Laravel DataTables UI and configuration helpers.
Boot a Laravel project on any machine with one command: app:serve installs missing tools (PHP, Node, Composer, Herd, Docker), creates .env, sets up the database, runs migrations, builds assets, starts a queue worker and serves via Herd, Sail or artisan serve; app:down cleanly stops everything it sta
Branded, diagnostic error pages (500, 403, 404, 419, 503) for Filament — native Filament UI, dark mode and translations out of the box.
统计信息
- 总下载量: 4.38k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 17
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-11-28