Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Advanced Otp Laravel Package

mkd/laravel-advanced-otp

View on GitHub
Deep Wiki
Context7

Laravel Advanced OTP

Latest Version on Packagist Total Downloads GitHub Actions

Laravel Advanced OTP is a package designed for flexible OTP (One-Time Password) verification, supporting both hashed token verification and custom validation methods. It allows for easy OTP handling for tasks like email-based authentication.


Features

  • Hashed Token Verification: Secure OTP validation using hashed tokens.
  • Custom Validation: Developers can use their own validation methods (e.g., database or cache-based).
  • Configurable OTP Settings: Custom timeout and OTP length.

Installation

Install the package via Composer:

composer require mkd/laravel-advanced-otp

Create your own OTPMethod

php artisan magic-otp:make LoginOTP

Usage

1. OTP Generation and Email Sending (Hashed Token)

In this example, a hashed token is used to securely send and verify the OTP.

// Generate OTP and send it via email
$otp = \LaravelAdvancedOTP::handle(LoginOTP::class, [
    'secret' => 'secret_key',  // Required to hash and verify OTP
    'email' => 'user_email@example.com',  // Email of the recipient
]);

// Get the hashed token for verification
$token = $otp->getHashedKey();

// Send OTP to user's email
$otp->send('user_email@example.com');

// Return the hashed token for later verification
return response()->json(['token' => $token]);

2. OTP Generation Without Hashed Token

If you want to handle the OTP validation manually (e.g., store it in a database or cache), you can omit the hashed token.

// Generate and send OTP without hashed token
\LaravelAdvancedOTP::handle(LoginOTP::class)->send('user_email@example.com');

3. Verifying OTP (Hashed Token)

Use the hashed token to validate the OTP.

$otp = request('otp');
$hashedToken = request('token');  // Token returned when sending OTP

$signature = [
    'secret' => 'secret_key',  // Same secret used during OTP generation
    'email' => 'user_email@example.com',
];

// Verify the OTP using the hashed token
$otpStatus = \LaravelAdvancedOTP::verify(LoginOTP::class, $otp, $signature, $hashedToken);

if ($otpStatus == OTPStatusEnum::NOT_VERIFIED) {
    // OTP is invalid
}

if ($otpStatus == OTPStatusEnum::VERIFIED) {
    // OTP is valid
}

if ($otpStatus == OTPStatusEnum::EXPIRED) {
    // OTP has expired
}

4. Verifying OTP (Custom Validation)

If you want to handle OTP validation manually, you can use your custom logic for verification.

$otp = request('otp');
$email = request('email');

// Custom validation for OTP
$otpVerified = \LaravelAdvancedOTP::validate(LoginOTP::class, $otp, $email);

if ($otpVerified) {
    // OTP is valid
} else {
    // OTP is invalid or expired
}

Custom OTP Class

To implement your OTP logic, create a class extending MagicOTP. Here is an example:

class LoginOTP extends MagicOTP
{
    protected int $timeout = 120;  // Timeout in seconds
    protected int $otpLength = 5;  // Length of the OTP

    public function send($email)
    {
        $otp = $this->getOTP();
        // Logic to send OTP via email
    }

    public function validate($otp, $email)
    {
        // Logic to validate OTP for the email
    }
}

Configuration

You can adjust the default settings like OTP timeout, length, and more by customizing your OTP class.

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email mustafakhaleddev@gmail.com instead of using the issue tracker.

Credits

License

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

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope