ahmedebead/laramultiauth

LaraMultiAuth 是一个灵活的 Laravel 扩展包,旨在简化在单个 Laravel 应用程序中实现多个身份验证系统。它提供了一种简化的解决方案,用于管理不同类型的用户身份验证,让您轻松支持各种用户角色和身份验证方法。它支持 API 和 Web 身份验证,包括多守卫设置和邮箱/电话的OTP验证。

v1.1.4 2024-08-20 01:11 UTC

This package is auto-updated.

Last update: 2024-09-20 01:17:46 UTC


README

LaraMultiAuth Logo

1. 简介

LaraMultiAuth 是一个灵活的 Laravel 扩展包,旨在简化在单个 Laravel 应用程序中实现多个身份验证系统。它提供了一种简化的解决方案,用于管理不同类型的用户身份验证,让您轻松支持各种用户角色和身份验证方法。它支持 API 和 Web 身份验证,包括多守卫设置和邮箱/电话的OTP验证。

2. 安装

要安装 LaraMultiAuth 扩展包,请按照以下步骤操作

  1. 将包添加到您的项目

    composer require ahmedebead/laramultiauth

设置

安装包后,运行以下命令以完成设置

php artisan multiauth:setup
php artisan passport:install # If needed 
  1. 注册服务提供者

    将以下行添加到 config/app.php 文件中的 providers 数组

    AhmedEbead\LaraMultiAuth\LaraMultiAuthServiceProvider::class,
  2. 发布配置文件

    php artisan vendor:publish --provider="AhmedEbead\LaraMultiAuth\LaraMultiAuthServiceProvider"

3. 配置

更新 config/multiauth.php 文件以配置模型和 SMS 辅助函数

<?php

return [
    'guards' => [
        'web' => [
            'model' => App\Models\User::class,
            'authFields' => [
                'username' => ['email','name'], // any number of fields name to check
                'password' => 'password'
            ]
        ],
        // Add more guards and their respective models here
    ],

    'sms_helper_function' => env('SMS_HELPER_FUNCTION', 'sendSmsHelperFunction'),

];

4. 使用

通过 LaraMultiAuth 门面使用包函数

使用邮箱/电话或其他字段登录 -- 检查配置字段

// api is the guard you can change it to web or any another

$token = LaraMultiAuth::guard('api')->login([
    'username' => 'user@example.com',
    'password' => 'password123',
]);

注册新用户

// api is the guard you can change it to web or any another

$user = LaraMultiAuth::guard('api')->register([
    'email' => 'newuser@example.com',
    'password' => 'password123',
    //.....other fields
]);

生成和验证 OTP

//By phone or email
// api is the guard you can change it to web or any another
$identifier = "010548569847"; // can be email $identifier = "test@mail.com";
$otp = LaraMultiAuth::guard('api')->generateOtp($identifier);

$isVerified = LaraMultiAuth::guard('api')->verifyOtp($identifier, $otp);

生成和发送 OTP

如果传递电话号码,将运行短信逻辑;如果传递邮箱,将发送到指定的邮箱

//By phone or email
// api is the guard you can change it to web or any another LaraMultiAuth::guard('api')
$otp = LaraMultiAuth::guard('api')->generateAndSendOtp('1234567890');

$otp = LaraMultiAuth::guard('api')->generateAndSendOtp('test@mail.com');

将返回

{
        "status": true,
        "message": "OTP is valid"
}

{
    "status": false,
    "message": "OTP is not valid"
}

获取当前登录用户数据

// api is the guard you can change it to web or any another

$token = LaraMultiAuth::guard('api')->loggedInUser();

登出

// api is the guard you can change it to web or any another

$token = LaraMultiAuth::guard('api')->logout();

忘记密码

// api is the guard you can change it to web or any another
//By phone or email
$token = LaraMultiAuth::guard('api')->forgetPassword($email);
$token = LaraMultiAuth::guard('api')->forgetPassword($phone);

重置密码

//By phone or email
// api is the guard you can change it to web or any another
$data = [
    'identifier'=>'test@test.com', // can be phone number
    'password'=>'password'
    'otp'=>125487
];
$token = LaraMultiAuth::guard('api')->resetPassword($data);

或者,您还可以添加任何字段名称,但您必须使用此字段数据创建 OTP

// api is the guard you can change it to web or any another

$otp = LaraMultiAuth::guard('api')->generateOtp('Ahmed Ebead');


$data = [
    'identifier_field_name'=>'name'
    'identifier'=>'ahmed ebead',
    'password'=>'password'
    'otp'=>125487
];
$token = LaraMultiAuth::guard('api')->resetPassword($data);

如果您不需要在密码重置时验证 OTP,则只需将第二个参数传递为 false 即可

// api is the guard you can change it to web or any another
$data = [
    'password'=>'password'
];
$token = LaraMultiAuth::guard('api')->resetPassword($data,false);

5. 模型实现

BaseAuthModel

AhmedEbead\LaraMultiAuth\Models\BaseAuthModel 提供了常见的身份验证功能。

具体模型

为每个守卫扩展 BaseAuthModel

Web 用户模型

<?php

namespace App\Models;

use AhmedEbead\LaraMultiAuth\Models\BaseAuthModel;

class WebUser extends BaseAuthModel
{
    protected $guard = 'web';
}

API 用户模型

<?php

namespace App\Models;

use AhmedEbead\LaraMultiAuth\Models\BaseAuthModel;

class ApiUser extends BaseAuthModel
{
    protected $guard = 'api';
}

管理员用户模型

<?php

namespace App\Models;

use AhmedEbead\LaraMultiAuth\Models\BaseAuthModel;

class AdminUser extends BaseAuthModel
{
    protected $guard = 'admin';
}

6. 辅助函数

示例 SMS 辅助函数

将此辅助函数添加到您的项目作为辅助函数以处理短信发送

if (!function_exists('sendSmsHelperFunction')) {
    function sendSmsHelperFunction($phone, $otp)
    {
        // Implement the SMS sending logic
        // Example: using an external SMS service
        // SmsService::send($phone, "Your OTP is: {$otp}");
    }
}

7. 创建辅助文件

A. 创建新的辅助文件

在 Laravel 10 中创建辅助函数的第一步是在 app/Helpers 目录中创建一个新文件。如果此目录不存在,则创建它。

B. 添加辅助函数

创建新辅助文件后,您可以添加您的辅助函数。辅助函数只是您可以从 Laravel 应用程序中的任何位置调用的常规 PHP 函数。

  • 例如
if (!function_exists('sendSmsHelperFunction')) {
    function sendSmsHelperFunction($phone, $otp)
    {
        // Implement the SMS sending logic
        // Example: using an external SMS service
        // SmsService::send($phone, "Your OTP is: {$otp}");
    }
}

C. 加载辅助文件

定义您的辅助函数后,您需要加载辅助文件,以便 Laravel 了解它。您可以通过向 Laravel 应用程序中的 composer.json 文件添加自动加载条目来完成此操作。打开 composer.json 文件,并在自动加载部分添加以下条目

{
    "files": [
        "app/Helpers/helper.php"
    ]
}

D. 使用辅助函数

现在,您已经定义了辅助函数并加载了辅助文件,您可以在 Laravel 应用程序中的任何地方使用该函数。例如,如果您想在控制器中格式化日期字符串,可以这样做

class OrderController extends Controller
{
    public function index()
    {
        sendSmsHelperFunction(....);
    }
}

8. API 和 Web 身份验证

该软件包支持API(使用Laravel Passport)和Web认证。保护类型决定了认证方法

  • Web认证:使用基于会话的标准登录。
  • API认证:使用Laravel Passport进行基于令牌的登录。

认证方法将根据配置中指定的保护类型动态处理。

9. 许可证

LaraMultiAuth软件包采用MIT许可证。有关更多信息,请参阅LICENSE文件。