nuwira / bandrek
Bandrek 是一个基于 Laravel 5.4.x 的密码恢复令牌生成器,使用数字作为代码。
Requires
- hashids/hashids: ^2.0
- laravel/framework: ~5.4
Requires (Dev)
- mockery/mockery: ^0.9.9
- nesbot/carbon: ^1.22
- phpunit/phpunit: ~5.7
README
Bandrek 是爪哇语中的当地词汇,意为 开锁工具。我们使用开锁工具来打开挂锁,如果丢失了钥匙。
Bandrek 替换了 Laravel 的密码恢复管理器,在请求密码恢复时提供代码和令牌。如果您使用 REST-API 来重置密码而不访问网页界面,则可以使用此代码作为令牌的替代。
Bandrek 生成 6 位随机数字作为代码。Bandrek 还创建 64 位字符的令牌,可以在常规网页界面中使用。
场景是当用户通过 REST-API 请求密码恢复时,用户可以发送凭据(电子邮件和密码)以及易于阅读的代码来重置密码。
安装
Bandrek 仅支持 Laravel 5.4 及以上版本。使用 Composer 安装,只需运行以下命令。
composer require nuwira/bandrek
配置
配置文件
安装后,打开 config/app.php
并找到此行。
Illuminate\Auth\Passwords\PasswordResetServiceProvider::class
注释或删除该行,并将该行添加到覆盖 Laravel 的密码重置处理。
Nuwira\Bandrek\BandrekServiceProvider::class
添加 Facade
这是可选的。要在 config/app.php
的 aliases
部分中添加 Facade,请这样做。
'Bandrek' => Nuwira\Bandrek\BandrekFacade::class,
如果 Facade 已加载,您可以使用以下函数
Bandrek::getRandomCode($length = 6, $toString = true); Bandrek::generateToken(); Bandrek::getTokenFromCode($code); Bandrek::getCodeFromToken($token, $toString = true);
模型文件
为了能够发送重置密码的电子邮件指令,打开 app/User.php
(用户模型文件)并找到此行。
use Illuminate\Foundation\Auth\User as Authenticatable;
将此行替换为。
use Nuwira\Bandrek\Auth\User as Authenticatable;
使用自定义通知
默认情况下,Bandrek 使用电子邮件进行通知。您可以通过扩展抽象类 Nuwira\Bandrek\Notification\BandrekNotification
来添加或替换您喜欢的通知方法。令牌和代码在通知类中的 $this->code
和 $this->token
中可用。
例如,如果您想使用 Gammu SMS 通知 发送代码,只需安装并配置它。
要使用 SMS 和电子邮件发送代码,请在扩展 Nuwira\Bandrek\Auth\User
的模型中添加函数并注入通知。
通知文件: App\Notifications\ResetPassword.php
namespace App\Notifications; use Nuwira\Bandrek\Notification\BandrekNotification; use Illuminate\Notifications\Messages\MailMessage; use NotificationChannels\Gammu\GammuChannel; use NotificationChannels\Gammu\GammuMessage; class ResetPassword extends BandrekNotification { public function via($notifiable) { return ['mail', GammuChannel::class]; } public function toMail($notifiable) { return (new MailMessage) ->line('You are receiving this email because we received a password reset request for your account.') ->action('Reset Password', url(config('app.url').route('password.reset', $this->token, false))) ->line('If you did not request a password reset, no further action is required.'); } public function toGammu($notifiable) { return (new GammuMessage()) ->to($phoneNumber) ->content('To reset password, use this code: '.$this->code); } }
模型文件: App\User.php
namespace App; use Nuwira\Bandrek\Auth\User as BaseUser; use App\Notifications\ResetPassword as ResetPasswordNotification; class User extends BaseUser { public function sendPasswordResetNotification($token) { $this->notify(new ResetPasswordNotification($token)); } }
许可协议
MIT 许可协议 (MIT)。请参阅 许可文件 了解更多信息。