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

namespace App\Livewire\Auth;

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


class Login extends Component
{
    public $email;
    public $password;
    public $response;
    public $device_name;
    public $ip_address;

    protected $rules = [
        "email" => "required|email|exists:users,email",
        // "email" => "required|valid_email_domain|email|exists:users,email",
        "password" => "required|valid_password",
        "device_name" => "required|string",
        'ip_address' => 'sometimes',
    ];

    public function mount()
    {
        $this->device_name = Session::get('device_name');
        $country = Session::get('country');
        $this->ip_address = $country['ip'];
    }

    public function login()
    {
        try {
            $this->validate();

            $response = Http::post(ApiEndpoints::login(), [
                'email' => $this->email,
                'password' => $this->password,
                'device_name' => $this->device_name,
                'ip_address' => $this->ip_address,
            ]);

            if ($response->successful()) {
                $info = $response->json()['message'];
                $token = $response->json()['token'];
                $user = $response->json()['user'];
                session(['user' => $user]);
                session(['token' => $token]);

                Session::flash('success', $info);
                if ($user['role'] == 'seller') {
                    $shop = DB::table('shops')->where('user_id', $user['id'])->first();
                    $this->getSellerShop();
                    if (!$shop) {
                        return redirect()->to('/dashboard-shop'); // seller
                    }
                    $gig = DB::table('gigs')->where('user_id', $user['id'])->first();
                    if (!$gig) {
                        return redirect('/dashboard-gig');
                    }
                    $bank = DB::table('bank_details')->where('user_id', $user['id'])->first();
                    if (!$bank) {
                        return redirect('/dashboard-bank');
                    }
                    return redirect()->intended('/dashboard'); // seller
                } else {
                    return redirect()->intended('/services');
                }
            } else {
                $info = $response->json()['message'];
                $this->addError('password', $info);
                Session::flash('error', $info);
            }
        } catch (\Throwable $e) {
            $info = $e->getMessage();
            Session::flash('error', $info);
            Log::error($e->getMessage());
            $this->addError('password', 'Check your internet connection and please retry');

        }
    }

    public $user_id;
    private function getSellerShop()
    {
        $this->user_id = Session::get("user")['id'];

        $body = [
            'user_id' => $this->user_id,
        ];

        $apiEndpoints = new ApiEndpoints();
        $headers = $apiEndpoints->header();
        $response = Http::withHeaders($headers)
            ->withBody(json_encode($body), 'application/json')
            ->get(ApiEndpoints::shopInfo());
        if ($response->successful()) {
            $shop = $response->json()['data'];
            Session::put('shop', $shop);
            $shop = Session::get('shop');
            return redirect()->to('/dashboard-my-shop');
        } else {
            Session::flash('error', 'Failed get shop. Please try again.');
        }
    }
    public function logout()
    {
        Session::flush();
        return redirect()->to('/login');
    }

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