salimmbise/otp-library

一个简单的PHP OTP库

1.1.0 2024-09-20 14:25 UTC

This package is auto-updated.

Last update: 2024-09-25 09:18:16 UTC


README

如何安装包

composer require salimmbise/otp-library

如何发送OTP

首先确保您运行了composer require和发布Vendor,然后导入此包以便使用。以下是在Laravel应用程序中使用此库的示例:

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();
    }
});

如何验证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();
    }
});

编码愉快!