mahdialikhani/otp-authenticator

This package assists with authenticating users using OTP (One-Time Password) method.

0.0.2 2023-02-06 11:55 UTC

This package is auto-updated.

Last update: 2025-06-13 16:38:14 UTC


README

Social Card of Laravel Media Library

OTP Authenticator

GitHub release (latest SemVer) Packagist Downloads

The OTP Authenticator package provides a simple and efficient solution for ensuring the security of your users' accounts. With the OTP (One-Time Password) method, users can securely log in to their accounts without the fear of their credentials being compromised. The package is easy to install and integrates seamlessly with your Laravel application, making it a hassle-free experience for developers.


Installation

Requirements

  • PHP 8.0+
  • Laravel 8+

You can install the package via composer:

composer require mahdialikhani/otp-authenticator

and add the OTPAuthenticatorServiceProvider service provider to config/app.php

and then run:

php artisan vendor:publish --tag=otpauthenticator

php artisan otp:install

Our default support includes two text message service provider, Ghasedak and Kavenegar, with plans to expand our support to additional services. To use any of these services, simply follow the instructions provided.

Kavenegar:

To utilize the Kavenegar service, first follow the installation instructions for the Kavenegar package as outlined in the service provider's official documentation. Then, specify the SMS sender number in the config/otpauthenticator.php file.

'kavenegar' => [
    'line_number' => ''
]

and then:

You can edit the toSms function in the \App\Notifications\VerificationNotification class as follows:

return (new KavenegarMessage)
            ->to('09301111111')
            ->message('Hi dear, your verification code is:123456');

and done!

Ghasedak:

To utilize the ghasedak service, first follow the installation instructions for the ghasedak package provided in the official service provider's documentation. If you opt to use a pre-made text message template from the Ghasedak service, indicate the name of your selected template in the config/otpauthenticator.php file.

'ghasedak' => [
    'template_name' => ''
]

and then:

You can edit the toSms function in the \App\Notifications\VerificationNotification class as follows:

return (new GhasedakMessage)
            ->to('09301111111')
            ->message('123456');

and done!

You can use the Laravel documentation to use the vonage service or create a personalized service by following these steps:

  • Create a custom class in \App\Notifications\Messages that implements the Messageable interface.
  • Define the send method for sending your text message.
  • Use this class in the \App\Notifications\VerificationNotification file, similar to other services.

To conveniently access the recipient and message text, extend your message class from the "SimpleMessage" class.

for example:

<?php

namespace App\Notifications\Messages;

use Mahdialikhani\OtpAuthenticator\Contracts\Messageable;
use Mahdialikhani\OtpAuthenticator\Notifications\Messages\SimpleMessage;

class SmsirMessage extends SimpleMessage implements Messageable
{
    public function send()
    {
        // Code here
    }
}
<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Notification;
use Mahdialikhani\OtpAuthenticator\Notifications\Channels\SmsChannel;

class VerificationNotification extends Notification
{
    use Queueable;

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return [SmsChannel::class];
    }

    /**
     * Get the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toSms($notifiable)
    {
        return (new SmsirMessage)
            ->to('09301111111')
            ->message('123456');
    }
}

Also, if you don't want the verification code to be sent to the user via text message, you can apply the email settings in your .env file and make the following changes in the file \App\Notifications\VerificationNotification to send the validation code via email.

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class VerificationNotification extends Notification
{
    use Queueable;

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['mail'];
    }

    /**
     * Get the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        return (new MailMessage)
            ->line('The introduction to the notification.')
            ->action('Notification Action', url('/'))
            ->line('Thank you for using our application!');
    }
}

Credits

License

The MIT License (MIT). Please see License File for more information.