larakeeps/authguard-otp

AuthGuard OTP 是一个用于生成、验证和确认(OTP)的包。

v1.0.0 2024-02-04 00:28 UTC

This package is auto-updated.

Last update: 2024-09-05 16:30:52 UTC


README

AuthGuard OTP

AuthGuard OTP 是一个用于生成、验证和确认(OTP)的包。通常OTP与通过电子邮件或短信发送的验证码或令牌一起使用,以验证或授权项目中的某些操作。

此存储库仅与 laravel: 7.*11.* 兼容

安装

第一步,执行命令。

composer require larakeeps/authguard-otp

第二步,运行迁移以创建表: auth_guard_otp_codes

php artisan migrate

第三步,发布 authguard 配置,要发布配置,请运行以下命令

php artisan vendor:publish --tag=authguard-otp-config 

生成 OTP 码

use Larakeeps\AuthGuard\Facades\OTP;

/** 
 * 
 * The create method has 3 parameters, 1 mandatory and 2 optional.
 * 
 * The $reference parameter is used with the assertive condition function in code validation.
 * The $email parameter is used in the same way as the $reference parameter
 * 
 * @param string $phone : required
 * @param string $email : optional
 * @param string $reference : optional
 * 
 * @method static OTP create(string $phone, string|null $email, string|null $reference)
 * 
 * */

$createOTP = OTP::create('phone number', 'email', 'reference');

// To retrieve the method return, call the following methods below.

/** 
 * 
 * The then method returns 2 parameters, $data of type AuthGuard, and $response of type Collection.
 * 
 * @method then(Closure $destination)
 * 
 * */
 
$createOTP->then(function (AuthGuard|null $data, Collection $response){
       
       /*
        * the $data parameter returns the null or Authguard model containing the table columns.
        */
       
       /*
        * The $response parameter returns a collection containing the following data: 
        * number_digits, code, message, status, user.access_token, user.ip_address
        */
        
        return $data->expires_at; // returns a value of type Carbon::class;
        
        if($response->status){
            return $response->message;
        }
       
});

// OR through the get() method that returns a Collection

return $createOTP->get();

// OR like this

return OTP::get();


// OR through the getResponse() method that returns a Collection

return $createOTP->getResponse();

// OR like this

return OTP::getResponse();


// OR can be called individually using methods

return OTP::getData()->expires_at; // returns a value of type Carbon::class;

if(OTP::getStatus()){
    return OTP::getMessage();
}

return OTP::getAccessToken();
return OTP::getIpAddress();

检查生成码的存在并查看生成数据。

use Larakeeps\AuthGuard\Facades\OTP;



/** 
 * 
 * Method to check if the generated code exists.
 * $phone parameter is used for better code verification assertiveness.
 * 
 * @method static bool hasCode(string $code, string|null $phone)
 * 
 * */

$hasCode = OTP::hasCode('154896');

if($hasCode){
    return "The code exist."
}


/** 
 * 
 * Method for finding and returning data.
 * $phone parameter is used for better code verification assertiveness.
 * 
 * @method static OTP getByCode(string $code, string|null $phone)
 * 
 * */

$authGuardFounded = OTP::getByCode('154896', '5521985642205');

if($authGuardFounded){
    return $authGuardFounded
}

确认输入的码是否有效以及是否已实际确认。

use Larakeeps\AuthGuard\Facades\OTP;


/** 
 * 
 * The create method has 3 parameters, 2 mandatory and 1 optional.
 * 
 * The $reference parameter is used with the assertive condition function in code validation.
 * 
 * @param string $code : required
 * @param string $phone : required
 * @param string $reference : optional
 * 
 * @method static OTP confirm(string $code, string $phone, string|null $reference)
 * 
 * */
 
$validateCode = OTP::confirm('154896', '5521985642205'); 

// To retrieve the method return, call the following methods below.

/** 
 * 
 * The then method returns 2 parameters, $data of type AuthGuard, and $response of type Collection.
 * 
 * @method then(Closure $destination)
 * 
 * */
 
$validateCode->then(function (null $data, Collection $response){
       
       /*
        * 
        * Within the confirm() method, the $data parameter will always return a null value.
        * 
        * The $response parameter returns a collection containing the following data: 
        * number_digits, code, message, status, user.access_token, user.ip_address
        */
        
        if($response->status && OTP::isConfirmed()){
            return $response->message;
        }
        
        return $response->message;
       
});

// OR through the getResponse() method that returns a Collection

return $createOTP->getResponse();

// OR like this

return OTP::getResponse();

// OR can be called individually using methods

if(OTP::getStatus()){
    return OTP::getMessage();
}

return OTP::getAccessToken();
return OTP::getIpAddress();

删除 OTP 码。

use Larakeeps\AuthGuard\Facades\OTP;

/** 
 * 
 * @method OTP deleteCode(string $code)
 * 
 * */
 
$deletedCode = OTP::deleteCode();

// To retrieve the method return, call the following methods below.

/** 
 * 
 * The then method returns 2 parameters, $data of type AuthGuard, and $response of type Collection.
 * 
 * @method then(Closure $destination)
 * 
 * */
 
$validateCode->then(function (null $data, Collection $response){
       
       /*
        * 
        * Within the deleteCode() method, the $data parameter will always return a null value.
        * 
        * The $response parameter returns a collection containing the following data: 
        * number_digits, code, message, status, user.access_token, user.ip_address
        */
        
        if($response->status){
            return $response->message;
        }
       
});

// OR through the getResponse() method that returns a Collection

return $createOTP->getResponse();

// OR like this

return OTP::getResponse();

// OR can be called individually using methods

if(OTP::getStatus()){
    return OTP::getMessage();
}

return OTP::getAccessToken();
return OTP::getIpAddress();

别忘了在 github 上关注我并给项目加星。


我的联系方式

电子邮件: douglassantos2127@gmail.com

领英: 访问个人资料