GOOD SHELL MAS BOY
Server: Apache/2.4.52 (Ubuntu)
System: Linux vmi1836763.contaboserver.net 5.15.0-130-generic #140-Ubuntu SMP Wed Dec 18 17:59:53 UTC 2024 x86_64
User: www-data (33)
PHP: 8.4.10
Disabled: NONE
Upload Files
File: /var/www/html/app/Livewire/Auth/Register.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\Cache;
use Illuminate\Support\Facades\Session;

class Register extends Component
{
    public $name;
    public $lastname;
    public $middlename;
    public $dob;
    public $gender;
    public $phone;
    public $email;
    public $nationality_id;
    public $password;
    public $countries = [];
    public $device_name;
    public $latitude;
    public $ip;
    public $role = '';
    public $nin;
    public $longitude;
    public $showPassword = false;

    protected $rules = [
        "email" => "required|valid_email_domain|email|unique:users,email",
        // "nationality_id" => "required|exists:countries,id",
        "name" => "required|string",
        "password" => "required|valid_password",
        "role" => "required",
        "device_name" => "required|string",
    ];

    protected $messages = [
        "nationality_id.required" => "The country field is required.",
        "nationality_id.exists" => "The selected country is invalid.",
    ];

    public function mount()
    {
        $this->countries = Cache::remember('countries', 60 * 60 * 24 * 30, function () {
            return Http::get(ApiEndpoints::getCountry())->json()['data'];
        });
        $this->device_name = Session::get('device_name');
        $country = Session::get('country');
        $this->ip = $country['ip'];
        $this->latitude = $country['latitude'];
        $this->longitude = $country['longitude'];
    }

    public function updatedRole($value)
    {
        Log::info('Role updated to: ' . $value);
        $this->role = $value;
    }
    public function register()
    {
        try {
            $this->validate([
                "name" => "required|string|max:100",
                // "role" => "required|string|max:100",
                "email" => "required|email|unique:users,email",
                "password" => "required|valid_password",
            ]);

            $body = [
                'email' => $this->email,
                'name' => $this->name,
                'ip_address' => $this->ip,
                'role' => 'buyer',
                'nationality_id' => '1',
                'password' => $this->password,
                'device_name' => $this->device_name
            ];
            Session::put('body', $body);
            $this->redirect('/complete-registration');

            // $response = Http::post(ApiEndpoints::register(), $body);
            // if ($response->successful()) {
            //     $info = $response->json(['message']);

            //     $user = $response->json(['user']);
            //     $token = $response->json(['token']);
            //     session(['user' => $user]);
            //     session(['token' => $token]);

            //     session(['user_email' => $this->email]);

            //     Session::flash('success', $info);
            //     return redirect()->to('/complete-registration');
            // } else {
            //     $info = $response->json(['message']);
            //     $this->addError('name', $info);
            //     Session::flash('error', $info);
            // }
        } catch (\Throwable $th) {
            $info = $th->getMessage();
            Session::flash('error', $info);
            Log::error($th->getMessage());
            $this->addError('name', 'Check your internet connection');
        }
    }

    public function updated($propertyName)
    {
        $this->validateOnly($propertyName);
    }

    // private function verifyNIN()
    // {
    //     try {
    //         // Validate NIN
    //         $this->validate([
    //             'nin' => 'required|unique:seller_nins,nin|digits:11',
    //             "name" => "required|string|max:100",
    //             "email" => "required|email|unique:users,email",
    //             "password" => "required|valid_password",
    //             'nationality_id' => 'required|exists:countries,id',
    //         ], [
    //             'nationality_id.required' => 'The country field is required.',
    //             'nationality_id.exists' => 'The selected country is invalid.',
    //         ]);


    //         // Make API call to validate NIN
    //         $response = Http::post(ApiEndpoints::validateNIN(), [
    //             'nin' => $this->nin,
    //         ]);

    //         // If the response is successful, proceed with registration
    //         if ($response->successful()) {
    //             $data = $response->json()['body'];
    //             $this->name = $data['firstName'];
    //             $this->lastname = $data['lastName'];
    //             $this->middlename = $data['middleName'];
    //             $this->dob = $data['dateOfBirth'];
    //             $this->gender = $data['gender'];
    //             $this->phone = $data['mobileNumber'];
    //             session(['user_phone' => $this->phone]);
    //             // Notify success and proceed to registration
    //             $this->registerSeller();
    //             Session::flash('success', 'A 6-digit OTP has been sent to your phone number.');
    //             return redirect()->to('/verify-phone');
    //         } else {
    //             dd($response->json()['message']);
    //             $this->errorMessage = $response->json()['message'];
    //             $this->addError('nin', $response->json()['message']);
    //         }
    //     } catch (\Throwable $th) {
    //         $this->addError('nin', $th->getMessage());
    //         Session::flash('error', 'Failed to validate NIN: ' . $th->getMessage());
    //     }
    // }

    public $errorMessage;

    public function registerSeller()
    {
        $this->validate();
        try {
            $body = [
                'nin' => $this->nin,
                'name' => $this->name,
                'lastname' => $this->lastname,
                'middlename' => $this->middlename ?? '',
                'dob' => $this->dob,
                'email' => $this->email,
                'gender' => $this->gender,
                'phone' => $this->phone,
                'password' => $this->password,
                'nationality_id' => $this->nationality_id,
                'device_name' => $this->device_name,
                'role' => $this->role,
                'ip_address' => Session::get('country')['ip'],
            ];
            // Make API call to register seller
            $response = Http::post(ApiEndpoints::registerSeller(), $body);

            if ($response->successful()) {
                Session::flash('success', 'A 6-digit OTP has been sent to your phone number');
                return redirect()->to('/verify-phone');
            } else {
                throw new \Exception('Failed to register seller: ' . $response->json()['error']);
            }
        } catch (\Throwable $th) {
            $this->addError('nin', 'Failed to register seller: ' . $th->getMessage());
            Session::flash('error', 'Failed to register seller: ' . $th->getMessage());
        }
    }


    public function sendDeleteAccountOtp()
{
    try {
        $response = Http::post(ApiEndpoints::sendDeleteAccountOtp(), [
            'email' => $this->email,
        ]);

        if ($response->successful()) {
            Session::flash('success', 'A 6-digit OTP has been sent to your email.');
        } else {
            $this->addError('otp', $response->json()['message'] ?? 'Failed to send OTP.');
        }
    } catch (\Throwable $th) {
        $this->addError('otp', 'Error sending OTP: ' . $th->getMessage());
        Session::flash('error', 'Error sending OTP: ' . $th->getMessage());
    }
}


public $otp;

public function verifyDeleteAccountOtp()
{
    $this->validate([
        'otp' => 'required|digits:6',
    ]);

    try {
        $response = Http::post(ApiEndpoints::verifyDeleteAccountOtp(), [
            'email' => $this->email,
            'otp' => $this->otp,
        ]);

        if ($response->successful()) {
            Session::flash('success', 'Account deleted successfully.');
            auth()->logout();
            return redirect()->to('/login');
        } else {
            $this->addError('otp', $response->json()['message'] ?? 'Invalid OTP.');
        }
    } catch (\Throwable $th) {
        $this->addError('otp', 'Error verifying OTP: ' . $th->getMessage());
        Session::flash('error', 'Error verifying OTP: ' . $th->getMessage());
    }
}


    public function render()
    {
        return view('livewire.auth.register');
    }
}