converting store files done
This commit is contained in:
parent
6825ce3c30
commit
6db9461437
|
@ -3,7 +3,7 @@
|
||||||
namespace App\Livewire;
|
namespace App\Livewire;
|
||||||
|
|
||||||
use Livewire\Component;
|
use Livewire\Component;
|
||||||
use Illuminate\Support\Facades\Http;
|
use App\Services\ApiService;
|
||||||
use Illuminate\Support\Facades\Session;
|
use Illuminate\Support\Facades\Session;
|
||||||
|
|
||||||
class FetchData extends Component
|
class FetchData extends Component
|
||||||
|
@ -12,6 +12,13 @@ class FetchData extends Component
|
||||||
public $loading = false;
|
public $loading = false;
|
||||||
public $url;
|
public $url;
|
||||||
|
|
||||||
|
protected $apiService;
|
||||||
|
|
||||||
|
public function boot(ApiService $apiService)
|
||||||
|
{
|
||||||
|
$this->apiService = $apiService;
|
||||||
|
}
|
||||||
|
|
||||||
public function mount($url)
|
public function mount($url)
|
||||||
{
|
{
|
||||||
$this->url = $url;
|
$this->url = $url;
|
||||||
|
@ -23,15 +30,12 @@ class FetchData extends Component
|
||||||
$this->loading = true;
|
$this->loading = true;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$response = Http::get(config('app.api_base_url') . $this->url);
|
$data = $this->apiService->fetchData($this->url);
|
||||||
|
if ($data) {
|
||||||
if ($response->successful()) {
|
$this->data = $data;
|
||||||
$this->data = $response->json();
|
|
||||||
} else {
|
|
||||||
Session::flash('error', 'Failed to fetch data: ' . $response->body());
|
|
||||||
}
|
}
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
Session::flash('error', 'An error occurred while fetching data.');
|
Session::flash('error', $e->getMessage());
|
||||||
} finally {
|
} finally {
|
||||||
$this->loading = false;
|
$this->loading = false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,20 +3,26 @@
|
||||||
namespace App\Livewire;
|
namespace App\Livewire;
|
||||||
|
|
||||||
use Livewire\Component;
|
use Livewire\Component;
|
||||||
use Illuminate\Support\Facades\Http;
|
use App\Services\ApiService;
|
||||||
use Illuminate\Support\Facades\Session;
|
use Illuminate\Support\Facades\Session;
|
||||||
|
|
||||||
class Logout extends Component
|
class Logout extends Component
|
||||||
{
|
{
|
||||||
|
protected $apiService;
|
||||||
|
|
||||||
|
public function boot(ApiService $apiService)
|
||||||
|
{
|
||||||
|
$this->apiService = $apiService;
|
||||||
|
}
|
||||||
|
|
||||||
public function logout()
|
public function logout()
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
Http::withHeaders(['Authorization' => 'Bearer ' . Session::get('token')])->get(config('app.api_base_url') . '/logout');
|
if ($this->apiService->logout()) {
|
||||||
|
|
||||||
Session::forget('token');
|
|
||||||
return redirect()->route('login')->with('success', 'Logged out successfully.');
|
return redirect()->route('login')->with('success', 'Logged out successfully.');
|
||||||
|
}
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
return redirect()->back()->with('error', 'Logout failed: ' . $e->getMessage());
|
return redirect()->back()->with('error', $e->getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,23 +2,20 @@
|
||||||
|
|
||||||
namespace App\Providers;
|
namespace App\Providers;
|
||||||
|
|
||||||
|
use App\Services\ApiService;
|
||||||
use Illuminate\Support\ServiceProvider;
|
use Illuminate\Support\ServiceProvider;
|
||||||
|
|
||||||
class AppServiceProvider extends ServiceProvider
|
class AppServiceProvider extends ServiceProvider
|
||||||
{
|
{
|
||||||
/**
|
public function register()
|
||||||
* Register any application services.
|
|
||||||
*/
|
|
||||||
public function register(): void
|
|
||||||
{
|
{
|
||||||
//
|
$this->app->singleton(ApiService::class, function () {
|
||||||
|
return new ApiService();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function boot()
|
||||||
* Bootstrap any application services.
|
|
||||||
*/
|
|
||||||
public function boot(): void
|
|
||||||
{
|
{
|
||||||
//
|
// ...
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -0,0 +1,73 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Services;
|
||||||
|
|
||||||
|
use Illuminate\Support\Facades\Http;
|
||||||
|
use Illuminate\Support\Facades\Session;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
|
|
||||||
|
class ApiService
|
||||||
|
{
|
||||||
|
protected $baseUrl;
|
||||||
|
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
$this->baseUrl = config('app.api_base_url');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function fetchData($url, $token = null)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$response = Http::withHeaders([
|
||||||
|
'Authorization' => 'Bearer ' . ($token ?: Session::get('token', '')),
|
||||||
|
])->get($this->baseUrl . $url);
|
||||||
|
|
||||||
|
if ($response->successful()) {
|
||||||
|
return $response->json();
|
||||||
|
} else {
|
||||||
|
$this->handleError($response);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
Log::error('API Fetch Error: ' . $e->getMessage());
|
||||||
|
Session::flash('error', 'Failed to fetch data: ' . $e->getMessage());
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function logout($token = null)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$response = Http::withHeaders([
|
||||||
|
'Authorization' => 'Bearer ' . ($token ?: Session::get('token', '')),
|
||||||
|
])->get($this->baseUrl . '/logout');
|
||||||
|
|
||||||
|
if ($response->successful()) {
|
||||||
|
Session::forget('token');
|
||||||
|
Session::flash('success', 'Logged out successfully.');
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
$this->handleError($response);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
Log::error('Logout Error: ' . $e->getMessage());
|
||||||
|
Session::flash('error', 'Logout failed: ' . $e->getMessage());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function handleError($response)
|
||||||
|
{
|
||||||
|
$status = $response->status();
|
||||||
|
|
||||||
|
if ($status === 401) {
|
||||||
|
Session::forget('token');
|
||||||
|
throw new \Exception('Session expired. Please login again.');
|
||||||
|
} elseif ($status === 404) {
|
||||||
|
throw new \Exception('API resource not found.');
|
||||||
|
} else {
|
||||||
|
throw new \Exception('API error: ' . ($response->body() ?: 'Unknown error'));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue