1. Go to this page and download the library: Download webekspres/fonte-otp library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
webekspres / fonte-otp example snippets
namespace App\Http\Controllers;
use Webekspres\FonnteOtp\Facades\FonnteOtp;
use Illuminate\Http\Request;
class OtpController extends Controller
{
/**
* Send OTP to user's phone number
*/
public function sendOtp(Request $request)
{
$request->validate([
'phone' => ' } else {
return response()->json([
'message' => 'Failed to send OTP',
'error' => $response['error'] ?? 'Unknown error'
], 400);
}
} catch (\Exception $e) {
return response()->json([
'message' => 'Error sending OTP',
'error' => $e->getMessage()
], 500);
}
}
/**
* Send OTP with custom variables
*/
public function sendOtpWithVariables(Request $request)
{
$request->validate([
'phone' => 'e->getMessage()
], 500);
}
}
/**
* Verify OTP for user's phone number
*/
public function verifyOtp(Request $request)
{
$request->validate([
'phone' => '
// In routes/web.php or routes/api.php
use App\Http\Controllers\ProtectedController;
// Protect a route with OTP verification
Route::middleware(['verify.otp'])->post('/secure-action', [ProtectedController::class, 'handle']);
// Protect a route with both rate limiting and OTP verification
Route::middleware(['otp.rate', 'verify.otp'])->post('/sensitive-action', [ProtectedController::class, 'sensitive']);
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ProtectedController extends Controller
{
/**
* Handle protected action (OTP verification is done by middleware)
*/
public function handle(Request $request)
{
// At this point, OTP has been verified by middleware
// You can safely proceed with the protected action
return response()->json([
'message' => 'Protected action completed successfully',
'user_data' => [
'phone' => $request->input('phone'),
'action' => 'secure-action-completed'
]
]);
}
/**
* Handle sensitive action with rate limiting
*/
public function sensitive(Request $request)
{
// This action is protected by both rate limiting and OTP verification
return response()->json([
'message' => 'Sensitive action completed successfully',
'user_data' => [
'phone' => $request->input('phone'),
'action' => 'sensitive-action-completed'
]
]);
}
}
namespace App\Listeners;
use Webekspres\FonnteOtp\Events\OtpSent;
use Illuminate\Support\Facades\Log;
class LogOtpSent
{
/**
* Handle the event.
*/
public function handle(OtpSent $event): void
{
Log::info('OTP sent to phone number: ' . $event->phoneNumber, [
'otp' => $event->otp,
'timestamp' => now()
]);
}
}
use Webekspres\FonnteOtp\Events\OtpSent;
use App\Listeners\LogOtpSent;
protected $listen = [
OtpSent::class => [
LogOtpSent::class,
],
];
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class OtpResource extends JsonResource
{
/**
* Transform the resource into an array.
*/
public function toArray($request): array
{
return [
'phone_number' => $this->phone_number,
'expires_at' => $this->expires_at->toISOString(),
'created_at' => $this->created_at->toISOString(),
'is_expired' => $this->expires_at->isPast(),
];
}
}
use Webekspres\FonnteOtp\Facades\FonnteOtp;
// Send OTP to a phone number
$response = FonnteOtp::send('+6281234567890');
// Send OTP with custom variables
$variables = [
'name' => 'John Doe',
'action' => 'account verification'
];
$response = FonnteOtp::send('+6281234567890', $variables);
use Webekspres\FonnteOtp\Facades\FonnteOtp;
// Verify OTP for a phone number
$isValid = FonnteOtp::verify('+6281234567890', '123456');