salimmbise/otp-library

A simple OTP library for PHP

1.1.0 2024-09-20 14:25 UTC

This package is auto-updated.

Last update: 2025-04-25 10:42:08 UTC


README

How to Install the Package

composer require salimmbise/otp-library

How to Send OTP

At first make sure you run composer require and publish the Vendor, then import this package in order to use it. Below is example of how to use this library in Laravel Application:

use SalimMbise\OtpLibrary\OtpMailer;

Route::get('/send-otp', function () {
    try {
        // Instantiate the OtpMailer class
        $otpMailer = new OtpMailer();

        // Generate a test OTP
        $otp = rand(100000, 999999);

        // Use a test email to send the OTP
        $testEmail = 'test@example.com';

        // Send the OTP email
        $otpMailer->sendOtpEmail($testEmail, $otp);

        return "OTP email sent successfully to $testEmail with OTP: $otp";
    } catch (\Exception $e) {
        return "Failed to send OTP email. Error: " . $e->getMessage();
    }
});

How to Validate OTP

use SalimMbise\OtpLibrary\OtpService;

Route::get('/validate-otp', function () {
    try {
        // Instantiate the OtpService class
        $otpService = new OtpService();

        $email = $request->input('example@email.com');
        $otp = $request->input('340123');

        $isValid = $this->otpService->verifyOtp($email, $otp);

        if ($isValid) {
            return response()->json(['message' => 'OTP verified successfully']);

        } else {

            return response()->json(['message' => 'OTP verification failed'], 400);
        }

       
    } catch (\Exception $e) {
        return "Failed to send OTP email. Error: " . $e->getMessage();
    }
});

Happy Coding!