56 lines
1.4 KiB
PHP
56 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Services\Api\ApiService;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Session;
|
|
|
|
class AuthController extends Controller
|
|
{
|
|
protected ApiService $apiService;
|
|
|
|
public function __construct(ApiService $apiService)
|
|
{
|
|
$this->apiService = $apiService;
|
|
}
|
|
|
|
public function login(Request $request)
|
|
{
|
|
$credentials = $request->validate([
|
|
'email' => 'required|email',
|
|
'password' => 'required',
|
|
]);
|
|
|
|
try {
|
|
$response = $this->apiService->post('/auth/login', $credentials);
|
|
|
|
if ($response->successful()) {
|
|
$data = $response->json();
|
|
session(['api_token' => $data['token']]);
|
|
return redirect()->intended('/dashboard');
|
|
}
|
|
|
|
return back()->withErrors([
|
|
'email' => 'The provided credentials do not match our records.',
|
|
]);
|
|
} catch (\Exception $e) {
|
|
return back()->withErrors([
|
|
'error' => 'Unable to connect to the authentication service.',
|
|
]);
|
|
}
|
|
}
|
|
|
|
public function logout()
|
|
{
|
|
try {
|
|
$this->apiService->post('/auth/logout');
|
|
} catch (\Exception $e) {
|
|
// Log the error but proceed with local logout
|
|
}
|
|
|
|
Session::forget('api_token');
|
|
return redirect('/login');
|
|
}
|
|
}
|