Skip to content

Commit

Permalink
[Feature] Improve documentation about sms services (#29)
Browse files Browse the repository at this point in the history
* Improve documentation about sms services

* Fix styling

* Update README

---------

Co-authored-by: Baspa <Baspa@users.noreply.github.com>
  • Loading branch information
Baspa and Baspa authored Sep 10, 2024
1 parent 5ca9ae3 commit 39cec7d
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 31 deletions.
51 changes: 46 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
<?php

namespace App\Notifications;

use Vormkracht10\TwoFactorAuth\Notifications\SendOTP as NotificationsSendOTP;
use Illuminate\Notifications\Messages\VonageMessage;

class SendOTP extends NotificationsSendOTP
{
/**
* Get the Vonage / SMS representation of the notification.
*/
public function toVonage(mixed $notifiable): VonageMessage
{
return (new VonageMessage)
->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

Expand Down
14 changes: 8 additions & 6 deletions config/filament-two-factor-auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
|
*/
'options' => [
TwoFactorType::email,
TwoFactorType::phone,
TwoFactorType::authenticator,
TwoFactorType::email,
// TwoFactorType::phone,
],

'enabled_features' => [
Expand Down Expand Up @@ -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,

/*
|--------------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions resources/dist/filament-two-factor-auth.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 0 additions & 20 deletions src/Notifications/SendOTP.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -59,18 +51,6 @@ public function toMail(mixed $notifiable): Mailable
->to($notifiable->email);
}

/**
* Get the array representation of the notification.
*
* @return array<string, mixed>
*/
public function toArray(mixed $notifiable): array
{
return [
//
];
}

/**
* @throws IncompatibleWithGoogleAuthenticatorException
* @throws SecretKeyTooShortException
Expand Down

0 comments on commit 39cec7d

Please sign in to comment.