Skip to content

Laravel package to verify users with a one-time password (OTP)

License

Notifications You must be signed in to change notification settings

sanjabteam/verify

Repository files navigation

Sanjab Verify

Latest Stable Version Total Downloads Build Status Code Style Code Coverage License

Verify your user mobile/email with a one-time password.

Installation

You can install the package via composer:

composer require sanjabteam/verify

Publish config file using:

php artisan vendor:publish --provider=SanjabVerify\VerifyServiceProvider

Configuration

code: Unique code generator configs.

resend_delay : Resend delay between code sends in seconds.

expire_in: Expire sent code after minutes.

max_attemps: Max code check attempts.

max_resends: Maximum resends in one hour.

  • per_session: Maximum resends in one hour based on the user session. (Limitation: if user clear cookie)
  • per_ip: Maximum resends in one hour based on user IP. (Limitation: If two different user use one proxy)

Usage

Send code to the user

use Verify;
use App\Helpers\SmsVerifyMethod;

$result = Verify::request($request->input('mobile'), SmsVerifyMethod::class);

if ($result['success'] == false) { // If user exceed limitation
    return redirect()->back()->with('error', $result['message']); // Show error message
}

App\Helpers\SmsVerifyMethod is your send method class and you need to create that like this.

namespace App\Helpers;

use SanjabVerify\Contracts\VerifyMethod;

class SmsVerifyMethod implements VerifyMethod
{
    public function send(string $receiver, string $code)
    {
        // Send code to receiver
        if (send_sms($receiver, 'Your code is :'.$code) == 'success') {
            return true; // If code sent successfuly then return true
        }
        return false; // If send code failed return false
    }
}

Verify

You can verify code with request validation.

$request->validate([
    'code' => 'required|sanjab_verify:mobile'
]);

mobile is your receiver which in this case is mobile.

You can also verify it manually.

use Verify;

$result = Verify::verify($request->input('mobile'), $request->input('code'));
if ($result['success'] == false) {
    // Show error $result['message']
}

Note: You can verify a code just once. so if you need to check code in two different requests then you should use something like the session to handle that.

Contributing

Contributions are welcome!

License

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