masoudghadimi/two-factor-auth

此包已被弃用且不再维护。没有建议的替代包。

为laravel-ui提供的双因素认证

v1.0-beta.3 2021-04-09 11:06 UTC

This package is auto-updated.

Last update: 2021-07-07 12:37:00 UTC


README

Laravel/Ui的双因素认证

此包正在开发和调试中

安装

  1. 使用composer安装包

composer require masoudghadimi/two-factor-auth

  1. 发布配置文件

php artisan vendor:publish --tag=twoFactor

  1. 迁移

php artisan migrate

  1. 更新用户模型(app/Models/User)
protected $fillable = [
     'name',
     'email',
     'password',
     'phone_number',
     'two_factor_type'
];
  1. 将以下代码添加到用户模型(app/Models/User)中
public function verifyCodes()
{
    return $this->hasMany(VerifyCode::class);
}
  1. 将以下代码添加到登录控制器(app/Http/Auth/LoginController)中
use TwoFactorAuthenticate;

protected function authenticated(Request $request, $user)
{
     return $this->loggedIn($request , $user);
}

配置

打开配置文件 - config/twoFactor.php

  • 检测渠道
  • 更改路由前缀
  • 更改过期时间
  • 更改验证码中的数字位数

下一步

创建渠道文件,然后将其放入配置文件(notificationsChannels)中

例如 :

在app/channels中创建SmsVerifyCodeChannel.php,然后输入以下所需的代码

class SmsVerifyCodeChannel
{
    public function send($notifiable, Notification $notification)
    {
        if (! method_exists($notification , 'toSendVerifyCode')) {
            throw new \Exception('toSendVerifyCode not found');
        }

        $data = $notification->toSendVerifyCode($notifiable);

        $message = $data['message'];
        $phone = $data['number'];

        try{
            $lineNumber = 1111111;
            $api = new \Ghasedak\GhasedakApi('token');
            $api->SendSimple($phone, $message, $lineNumber);
        }
        catch(ApiException $e){
            echo $e->errorMessage();
        }
        catch(HttpException $e){
            echo $e->errorMessage();
        }
    }
}

接下来,进入config/twofactor.php文件,在notificationsChannels部分,输入以下创建的渠道

'notificationsChannels' => \App\channels\SmsVerifyCodeChannel::class,

最后一步

访问以下链接

localhost:8000/home/security

祝你好运