mahdialikhani/otp-authenticator

此软件包协助使用OTP(一次性密码)方法进行用户认证。

0.0.2 2023-02-06 11:55 UTC

This package is auto-updated.

Last update: 2024-09-13 15:07:15 UTC


README

Social Card of Laravel Media Library

OTP Authenticator

GitHub release (latest SemVer) Packagist Downloads

OTP Authenticator 软件包提供了一个简单且高效的解决方案,以确保用户账户的安全性。通过OTP(一次性密码)方法,用户可以安全地登录到他们的账户,无需担心其凭证被泄露。该软件包易于安装,并与Laravel应用程序无缝集成,为开发人员提供无烦恼的体验。


安装

需求

  • PHP 8.0+
  • Laravel 8+

您可以通过composer安装此软件包

composer require mahdialikhani/otp-authenticator

并将 OTPAuthenticatorServiceProvider 服务提供者添加到 config/app.php

然后运行

php artisan vendor:publish --tag=otpauthenticator

php artisan otp:install

我们的默认支持包括两个短信服务提供者,Ghasedak和Kavenegar,并计划扩展我们的支持到其他服务。要使用这些服务中的任何一项,请按照提供的说明操作。

Kavenegar

要利用Kavenegar服务,首先遵循服务提供者官方文档中概述的Kavenegar软件包的安装说明。然后,在 config/otpauthenticator.php 文件中指定短信发送者号码。

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

然后

您可以根据以下方式编辑 \App\Notifications\VerificationNotification 类中的 toSms 函数

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

完成!

Ghasedak

要利用ghasedak服务,首先遵循官方服务提供者文档中提供的ghasedak软件包的安装说明。如果您选择使用Ghasedak服务的预置短信模板,请在 config/otpauthenticator.php 文件中指定所选模板的名称。

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

然后

您可以根据以下方式编辑 \App\Notifications\VerificationNotification 类中的 toSms 函数

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

完成!

您可以使用Laravel文档使用 vonage 服务或通过以下步骤创建个性化的服务

  • \App\Notifications\Messages 中创建一个自定义类,该类实现了 Messageable 接口。
  • 定义发送方法以发送您的短信。
  • \App\Notifications\VerificationNotification 文件中使用此类,类似于其他服务。

为了方便访问接收者和消息文本,将您的消息类从 "SimpleMessage" 类扩展。

例如

<?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');
    }
}

此外,如果您不想通过短信将验证码发送给用户,您可以在 .env 文件中应用电子邮件设置,并在 \App\Notifications\VerificationNotification 文件中做出以下更改,通过电子邮件发送验证码。

<?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!');
    }
}

致谢

许可证

MIT许可证(MIT)。有关更多信息,请参阅 许可证文件