syehan / forgotpassword-plugin
允许忘记密码的用户解锁、恢复或重置密码。
v1.2.5
2023-10-27 19:16 UTC
Requires
- composer/installers: ~1.0
- spomky-labs/otphp: ^11.2
README
使用 syehan/forgotpassword-plugin
。允许忘记密码的用户通过回答账户安全问题或发送电子邮件来解锁、恢复或重置密码。此插件目前通过OTP进行验证。
安装
1 - 您可以通过composer安装此包
$ composer require syehan/forgotpassword-plugin
生成OTP的Secret Key Base32
在此插件中,我们有一些命令可以生成您的密钥的base32,之后将其保存到您的 config/syehan-otp-password.php
文件的 otp_secret_key
键中,如下所示
php artisan syehan:generate-otp-secret-key
// the output would be like this
Your Forgot Password OTP Secret Key is : Z566IV6FIMMANIKQQOIJDZNWREKJKAWKGTK3WGGBSFTOIOG4UFCN2QVDJNHUJKKT44JRDSPWTX6JNBYDGMIJHLKCD6UM4WJGFIVPU3VSLTXP6J45PG4V5Q2NMKY3H5FCXGXK4BAXHWX4PX3YDC6VYF5EB25GZJCS2LTKED5GA467HIEJHZW6XPVGXPQVMWITQVHILMDQHI7JE
使用方法
1 - 您可以使用此API发送忘记密码的电子邮件
POST https://yourdomain.com/api/syehan/forgot-password
添加 email
参数将忘记密码邮件发送到您的账户。
2 - 或者您可以将此函数放入您的函数中
use Syehan\ForgotPassword\Classes\ForgotMailMaker; (new ForgotMailMaker)->setEmail($email)->hit(); // You also can change the mail template and mail data for your custom mail like this below. Also we will ensure that otp code and email user added into your mail data. (new ForgotMailMaker)->setMailTemplateCode('author.plugin::mail.forgot')->setMailData(['data' => 'test'])->setEmail($email)->hit(); // You also can change send mail mode in queue() rather than send(), just change the var like this : (new ForgotMailMaker)->setEmail($email)->hit('queue'); //<- only allow set `queue` or `send` mode
使用方法(验证OTP)
1 - 您可以在您的函数中这样验证OTP密码
$is_otp_match = (new \Syehan\ForgotPassword\Classes\OtpMaker)->setIssuer($email)->verifyOtp($input_otp);
使用方法(更改密码)
1 - 通过此插件,您也可以在验证后使用此API进行密码更改
POST https://yourdomain.com/api/syehan/change-password
添加 email
、password
和 password_confirmation
参数以确保更改密码操作成功。此外,您可以在更改密码的同时验证OTP,通过添加 otp
请求参数。
或者,您可以将此函数放入任何函数中,如下所示
use Syehan\ForgotPassword\Classes\ChangePasswordMaker; (new ChangePasswordMaker) ->setEmail($email) ->setPassword($password) ->setPasswordConfirmation($password_confirmation) ->change(); // You can verify OTP simultaneously when changing password like this (new ChangePasswordMaker) ->setEmail($email) ->setPassword($password) ->setPasswordConfirmation($password_confirmation) ->withVerifyOtp(true, $otp_input) ->change();
最后,您可以通过更改 config/syehan-forgot-password.php
中的 user_model
来更改您的用户模型
配置忘记密码
如果您有任何关于忘记密码的设置,请确保复制以下配置并粘贴到 config/syehan-forgot-password.php
。
<?php return [ /** * The User Model, default is rainlab.user plugin. * this would be the model that allow us to change the password. * */ 'user_model' => env('SYEHAN_FORGOT_PASSWORD_USER_MODEL', \RainLab\User\Models\User::class), /** * The OTP secret key, identity of your app */ 'otp_secret_key' => env('SYEHAN_FORGOT_PASSWORD_OTP_SECRET_KEY', 'XFT35ETTPHPIBIAMIUEZ7SRE5K4YZLSQP3LU4DZFWW7NDUSRSGAR3JK2ETCXY4BYQIQQQRLX4GI2ZSUT4YQDWEEPMAMI75IHN6NBKBQYCCKPQZGBTJQJYBIBU4LGEBGMVRUW6XZFVSOUUVRL66NFIZ55CH7GIGWUJ5DMR2JRYCTMXUN2ZMVFCBWEJNOOJIMGLIAGZXIJOVGIY'), /** * The OTP issuer */ 'otp_issuer' => env('SYEHAN_FORGOT_PASSWORD_OTP_ISSUER', 'SyehanProductIssuer'), /** * Length of OTP digits, by default is 6. */ 'otp_digits' => env('SYEHAN_FORGOT_PASSWORD_OTP_DIGITS', 6), /** * The OTP digest algorithm, by default is sha1. */ 'otp_algorithm' => env('SYEHAN_FORGOT_PASSWORD_OTP_ALGORITHM', 'sha1'), /** * The Interval of OTP will be valid, by default is 60 second. */ 'otp_period' => env('SYEHAN_FORGOT_PASSWORD_OTP_PERIOD', 60), /** * Company Name is basically for using our default mail template which must include the company name. * Otherwise, the mail looks like sent by empty company. */ 'company_name' => env('SYEHAN_FORGOT_PASSWORD_COMPANY_NAME', 'Syehan Company'), /** * Company Site/Website is basically for using our default mail template which must include the company link. */ 'company_site' => env('SYEHAN_FORGOT_PASSWORD_COMPANY_SITE', 'syehan.com'), /** * Company Location is basically for using our default mail template which must include the company location. */ 'company_location' => env('SYEHAN_FORGOT_PASSWORD_COMPANY_LOCATION', 'Jakarta, Indonesia'), /** * Company Sender Name is basically for using our default mail template which must include the company sender name. */ 'company_sender_name' => env('SYEHAN_FORGOT_PASSWORD_COMPANY_SENDER_NAME', 'Syehan CS'), ];