From 39cec7d4806957cc04c420ef151d705f1898a521 Mon Sep 17 00:00:00 2001 From: Bas van Dinther Date: Tue, 10 Sep 2024 19:47:47 +0200 Subject: [PATCH] [Feature] Improve documentation about sms services (#29) * Improve documentation about sms services * Fix styling * Update README --------- Co-authored-by: Baspa --- README.md | 51 +++++++++++++++++++--- config/filament-two-factor-auth.php | 14 +++--- resources/dist/filament-two-factor-auth.js | 1 + src/Notifications/SendOTP.php | 20 --------- 4 files changed, 55 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index a0913c8..cfd6637 100644 --- a/README.md +++ b/README.md @@ -96,7 +96,8 @@ protected function casts(): array } ``` -> ❗ When using `fillable` instead of `guarded` on your model, make sure to add `two_factor_type` to the `$fillable` array. +> [!WARNING] +> When using `fillable` instead of `guarded` on your model, make sure to add `two_factor_type` to the `$fillable` array. ### Register the event listener @@ -158,18 +159,58 @@ You can simply add or remove (comment) the methods you want to use: ```php return [ 'options' => [ - TwoFactorType::email, - TwoFactorType::phone, TwoFactorType::authenticator, + TwoFactorType::email, + // TwoFactorType::phone, ], - 'sms_service' => null, // For example: MessageBird::class + 'sms_service' => null, // For example 'vonage', 'twilio', 'nexmo', etc. + 'send_otp_class' => null, ]; ``` If you want to use the SMS method, you need to provide an SMS service. You can check the [Laravel Notifications documentation](https://laravel-notification-channels.com/about/) for ready-to-use services. -**Also make sure your user model has a `phone` attribute.** +#### Example with Vonage + +Like the example in the [Laravel documentation](https://laravel.com/docs/11.x/notifications#formatting-sms-notifications) you need to create the `toVonage()` method in your notification class. That's why we recommend creating a custom notification class that extends the original `SendOTP` class from this package: + +```php +content('Your OTP is: ' . $this->getTwoFactorCode($notifiable)); + } +} +``` + +You can get the two factor code for the user by calling the `getTwoFactorCode` method on the notification class. + +Then you need to set the `send_otp_class` in the `config/filament-two-factor-auth.php` file: + +```php +return [ + // ... + + 'sms_service' => 'vonage', + 'send_otp_class' => App\Notifications\SendOTP::class, +]; +``` + +> [!NOTE] +> Make sure your user or notifiable model has a `routeNotificationForVonage` method that returns the phone number. Please check the documentation of the SMS service you're using for more information. ### Customization diff --git a/config/filament-two-factor-auth.php b/config/filament-two-factor-auth.php index f80b649..313efba 100644 --- a/config/filament-two-factor-auth.php +++ b/config/filament-two-factor-auth.php @@ -23,9 +23,9 @@ | */ 'options' => [ - TwoFactorType::email, - TwoFactorType::phone, TwoFactorType::authenticator, + TwoFactorType::email, + // TwoFactorType::phone, ], 'enabled_features' => [ @@ -57,12 +57,14 @@ | SMS Service |-------------------------------------------------------------------------- | - | This value determines which SMS service to use. For ready-to-use notification - | channels, you can check out the documentation (SMS) here: - | https://laravel-notification-channels.com/ + | To use an SMS service, you need to install the corresponding package. + | You then have to create a App\Notifications\SendOTP class that extends + | the Vormkracht10\TwoFactorAuth\Notifications\SendOTP class. After that, + | you can set the class alias in the sms_service key. | */ - 'sms_service' => null, // For example: MessageBird::class + 'sms_service' => null, // For example 'vonage', 'twilio', 'nexmo', etc. + 'send_otp_class' => null, /* |-------------------------------------------------------------------------- diff --git a/resources/dist/filament-two-factor-auth.js b/resources/dist/filament-two-factor-auth.js index e69de29..38d4cc9 100644 --- a/resources/dist/filament-two-factor-auth.js +++ b/resources/dist/filament-two-factor-auth.js @@ -0,0 +1 @@ +//# sourceMappingURL=data:application/json;base64,ewogICJ2ZXJzaW9uIjogMywKICAic291cmNlcyI6IFtdLAogICJzb3VyY2VzQ29udGVudCI6IFtdLAogICJtYXBwaW5ncyI6ICIiLAogICJuYW1lcyI6IFtdCn0K diff --git a/src/Notifications/SendOTP.php b/src/Notifications/SendOTP.php index eee1447..9b5f808 100644 --- a/src/Notifications/SendOTP.php +++ b/src/Notifications/SendOTP.php @@ -17,14 +17,6 @@ class SendOTP extends Notification implements ShouldQueue { use Queueable; - /** - * Create a new notification instance. - */ - public function __construct() - { - // - } - /** * Get the notification's delivery channels. * @@ -59,18 +51,6 @@ public function toMail(mixed $notifiable): Mailable ->to($notifiable->email); } - /** - * Get the array representation of the notification. - * - * @return array - */ - public function toArray(mixed $notifiable): array - { - return [ - // - ]; - } - /** * @throws IncompatibleWithGoogleAuthenticatorException * @throws SecretKeyTooShortException