File: /var/www/html/app/Livewire/Auth/VerifyPhone.php
<?php
namespace App\Livewire\Auth;
use Livewire\Component;
use App\Services\ApiEndpoints;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Session;
class VerifyPhone extends Component
{
public $phone_code1;
public $phone_code2;
public $phone_code3;
public $phone_code4;
public $phone_code5;
public $phone_code6;
public $phone_code;
public $phone;
public function mount()
{
$this->phone = session('user_phone');
}
public function verifyPhone()
{
$this->phone = session('user_phone');
$this->phone_code = $this->phone_code1 . $this->phone_code2 . $this->phone_code3 . $this->phone_code4 . $this->phone_code5 . $this->phone_code6;
try {
$this->validate([
"phone" => "required|exists:otp_verifications,phone",
"phone_code" => "required|digits:6|exists:otp_verifications,otp",
], [
'phone.exists' => 'The phone is not correct.',
'phone_code.exists' => 'The otp is not correct.',
'phone.required' => 'The phone field is required.',
'phone_code.required' => 'The otp field is required.',
'phone.digits' => 'The phone must be 11 digits.',
'phone_code.digits' => 'The otp must be 6 digits.',
]);
$body = [
'phone' => $this->phone,
'otp' => $this->phone_code,
];
$response = Http::post(ApiEndpoints::verifyPhoneOtp(), $body);
if ($response->successful()) {
$info = $response->json()['message'];
// Session::forget('user_phone');
// $token = $response->json()['token'];
// $user = $response->json()['user'];
// Session::put('token', $token);
// Session::put('user', $user);
// return redirect()->to('/dashboard-bank');
session()->flash('success', $info);
return redirect()->to('/download-app');
} else {
$info = $response->json()['message'];
$this->addError('phone_code', $info);
session()->flash('error', $info);
return redirect()->to('/verify-phone');
}
} catch (\Throwable $th) {
$info = $th->getMessage();
session()->flash('error', $info);
Log::error($th->getMessage());
$this->addError('phone_code', 'Check your internet connection and try again');
}
}
public function resendOtp()
{
try {
$this->phone = Session::get('user_phone');
$body = [
'phone' => $this->phone,
];
$response = Http::post(ApiEndpoints::sendPhoneOtp(), $body);
if ($response->successful()) {
session()->flash('success', 'Otp sent successfully to your phone');
Session::get('user_phone');
return redirect()->to('/verify-phone');
} else {
$info = $response->json()['message'];
$this->addError('phone_code', $info);
session()->flash('error', $info);
return redirect()->to('/verify-phone');
}
} catch (\Throwable $th) {
$info = $th->getMessage();
session()->flash('error', $info);
Log::error($th->getMessage());
$this->addError('phone_code', $info);
}
}
public function render()
{
return view('livewire.auth.verify-phone');
}
}