57 lines
1.5 KiB
PHP
57 lines
1.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Modules\Auth;
|
|
|
|
use App\Core\{Request, Response};
|
|
use App\Modules\Auth\AuthService;
|
|
use Throwable;
|
|
|
|
final class AuthController
|
|
{
|
|
public function __construct(private readonly AuthService $authService) {}
|
|
|
|
public function login(Request $request): void
|
|
{
|
|
$email = $request->input('email');
|
|
$password = $request->input('password');
|
|
|
|
if (!$email || !$password) {
|
|
Response::error('يرجى إدخال البريد الإلكتروني وكلمة المرور', 'VALIDATION_ERROR', 422);
|
|
return;
|
|
}
|
|
|
|
try {
|
|
$result = $this->authService->login($email, $password);
|
|
|
|
// Set refresh token in HttpOnly cookie
|
|
setcookie('refresh_token', $result['refresh_token'], [
|
|
'expires' => time() + (60 * 60 * 24 * 7),
|
|
'path' => '/api/v1/auth/refresh',
|
|
'httponly' => true,
|
|
'samesite' => 'Strict',
|
|
'secure' => true
|
|
]);
|
|
|
|
unset($result['refresh_token']);
|
|
|
|
Response::json([
|
|
'success' => true,
|
|
'data' => $result,
|
|
'message' => 'تم تسجيل الدخول بنجاح'
|
|
]);
|
|
} catch (Throwable $e) {
|
|
Response::error($e->getMessage(), 'AUTH_FAILED', 401);
|
|
}
|
|
}
|
|
|
|
public function me(Request $request): void
|
|
{
|
|
Response::json([
|
|
'success' => true,
|
|
'data' => $request->user
|
|
]);
|
|
}
|
|
}
|