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/Profile/SetPin.php
<?php

namespace App\Livewire\Profile;

use Livewire\Component;
use App\Services\ApiEndpoints;
use App\Services\DeviceService;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Session;

class SetPin extends Component
{
    public $pin;
    public $device_name;
    public $user_id;
    public $showPin;

    protected $rules = [
        "pin" => "required|digits:4",
    ];

    public function mount(DeviceService $deviceService)
    {
        try {
            $this->device_name = $deviceService->getDeviceName();
            $user = Session::get('user');
            $this->user_id = $user['id'];
        } catch (\Exception $e) {
            $this->addError('pin', $e->getMessage());
            Session::flash('error', $e->getMessage());
        }
    }
 
    public function setPin()
    {
        $this->validate();
        try {
            $response = Http::post(ApiEndpoints::setPin(), [
                'pin' => $this->pin,
                'device_name' => $this->device_name,
                'user_id' => $this->user_id,
            ]);
            if ($response->successful()) {
                $info = $response->json(['message']);
                // Retrieve the current session data
                $userData = Session::get('user');

                // Update the is_pin value
                $userData['is_pin'] = 1;

                // Save the updated data back into the session
                Session::put('user', $userData);
                Session::flash('success', $info);
                $this->addError('response', $info);
            } else {
                $info = $response->json(['message']);
                $this->addError('pin', $info);
                Session::flash('error', $info);
            }
        } catch (\Throwable $e) {
            $info = $e->getMessage();
            Session::flash('error', $info);
            Log::error($e->getMessage());
            $this->addError('email', $info);
        }
    }
    
    public function render()
    {
        return view('livewire.profile.set-pin');
    }
}