otp working
This commit is contained in:
56
app/Helpers/helpers.php
Normal file
56
app/Helpers/helpers.php
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Http\Client\PendingRequest;
|
||||||
|
use Illuminate\Support\Facades\Http;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
|
|
||||||
|
if (! function_exists('sendSMS')) {
|
||||||
|
/**
|
||||||
|
* Send a sms
|
||||||
|
*/
|
||||||
|
function sendSMS(string|int $phone, string|int $message): mixed
|
||||||
|
{
|
||||||
|
$response = Http::retry(
|
||||||
|
times: 3,
|
||||||
|
sleepMilliseconds: 50,
|
||||||
|
throw: false,
|
||||||
|
when: function (Exception $exception, PendingRequest $request) {
|
||||||
|
Log::error('Exception: ', [
|
||||||
|
'message' => $exception->getMessage(),
|
||||||
|
'line' => $exception->getLine(),
|
||||||
|
]);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
)
|
||||||
|
->post('http://216.250.14.144:3000/api/data', [
|
||||||
|
'phone' => '+993' . $phone,
|
||||||
|
'code' => $message,
|
||||||
|
]);
|
||||||
|
|
||||||
|
return $response->body();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! function_exists('unmask_phone')) {
|
||||||
|
/**
|
||||||
|
* Unmask Turkmenistan phone number from TM code +993 6X XX XX XX to 6xxxxxxx
|
||||||
|
*/
|
||||||
|
function unmask_phone(string $phone): string
|
||||||
|
{
|
||||||
|
// Keep digits only
|
||||||
|
$digits = preg_replace('/\D+/', '', $phone);
|
||||||
|
|
||||||
|
// Remove Turkmenistan country code if present
|
||||||
|
if (str_starts_with($digits, '993')) {
|
||||||
|
$digits = substr($digits, 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return only valid 8-digit TM mobile number
|
||||||
|
if (preg_match('/^6\d{7}$/', $digits)) {
|
||||||
|
return $digits;
|
||||||
|
}
|
||||||
|
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -23,13 +23,13 @@ class VerificationController extends Controller
|
|||||||
'phone.regex' => 'The phone number must be in the format: +993 KX XX XX XX (where K is 6 or 7).'
|
'phone.regex' => 'The phone number must be in the format: +993 KX XX XX XX (where K is 6 or 7).'
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$phone = $request->phone;
|
$phone = unmask_phone($request->phone);
|
||||||
|
|
||||||
// Generate a 4-digit OTP
|
// Generate a 4-digit OTP
|
||||||
$otp = (string) rand(1000, 9999);
|
$otp = (string) rand(1000, 9999);
|
||||||
|
|
||||||
// Log it to simulate SMS
|
// Log it to simulate SMS
|
||||||
Log::info("OTP for {$phone} is {$otp}");
|
Log::info('Tassyklaýyş belgi: ' . $otp);
|
||||||
|
|
||||||
// Store in DB
|
// Store in DB
|
||||||
PhoneVerification::updateOrCreate(
|
PhoneVerification::updateOrCreate(
|
||||||
@@ -41,6 +41,8 @@ class VerificationController extends Controller
|
|||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
|
sendSMS($phone, $otp);
|
||||||
|
|
||||||
// Store phone in session for the next step
|
// Store phone in session for the next step
|
||||||
session()->put('verify_phone', $phone);
|
session()->put('verify_phone', $phone);
|
||||||
|
|
||||||
|
|||||||
@@ -24,7 +24,10 @@
|
|||||||
"App\\": "app/",
|
"App\\": "app/",
|
||||||
"Database\\Factories\\": "database/factories/",
|
"Database\\Factories\\": "database/factories/",
|
||||||
"Database\\Seeders\\": "database/seeders/"
|
"Database\\Seeders\\": "database/seeders/"
|
||||||
}
|
},
|
||||||
|
"files": [
|
||||||
|
"app/Helpers/helpers.php"
|
||||||
|
]
|
||||||
},
|
},
|
||||||
"autoload-dev": {
|
"autoload-dev": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
|
|||||||
Reference in New Issue
Block a user