Files
Hermes Agent 5d87e4975c Add HttpOnly cookie auth for cross-domain SPA login
Problem: Frontend JS bundle used credentials:'same-origin' which
dropped cookies on cross-domain requests. Login worked (200) but
the subsequent /auth/me check returned 401, leaving the user stuck
on the login screen.

Fix:
- AuthController now sets a dtp_jwt HttpOnly cookie on login/refresh
- Cookie uses Domain=.fahrschultermin.de (shared between frontend
  and api subdomain), Secure, SameSite=Lax, Max-Age=8h
- JwtMiddleware reads JWT from Authorization header OR cookie
- Added AuthController::me() endpoint (was missing, caused 500)
- Logout endpoint clears the cookie
- Frontend index.php patches fetch() to use credentials:'include'
  for all /api/v1/* calls
2026-06-04 20:33:31 +02:00

77 lines
2.9 KiB
PHP
Executable File

<?php
declare(strict_types=1);
namespace App\Http\Controllers;
use App\Repositories\AppointmentRepository;
use App\Repositories\InstructorRepository;
use App\Repositories\ReferenceDataRepository;
use App\Repositories\StudentRepository;
use App\Repositories\TenantRepository;
use App\Repositories\UserRepository;
use App\Services\RequirementService;
use App\Services\HolidayService;
use App\Services\SunTimesService;
use App\Support\Auth;
use App\Support\Request;
use App\Support\Response;
final class BootstrapController
{
public function __invoke(Request $request): void
{
$user = Auth::requireUser();
unset($user['password_hash']);
$payload = ['session' => ['user' => $user]];
if ($user['role'] === 'superadmin') {
$payload['tenants'] = (new TenantRepository())->all();
$payload['users'] = (new UserRepository())->allUsers();
Response::json($payload);
}
$tenantId = (int) $user['tenant_id'];
$studentRepository = new StudentRepository();
$students = $studentRepository->allForTenant($tenantId);
$requirementService = new RequirementService();
foreach ($students as &$student) {
$student['progress'] = $requirementService->progressForStudent((int) $student['id']);
}
$payload['tenant'] = (new TenantRepository())->find($tenantId);
$payload['students'] = $students;
$payload['templates'] = (new ReferenceDataRepository())->templates($tenantId);
$payload['lessonTypes'] = (new ReferenceDataRepository())->lessonTypes($tenantId);
$payload['instructors'] = (new InstructorRepository())->allForTenant($tenantId);
$payload['users'] = $user['role'] === 'tenant_admin'
? (new UserRepository())->allForTenant($tenantId)
: [];
$instructorId = null;
if ($user['role'] === 'instructor') {
$instructor = (new InstructorRepository())->findByUserId($tenantId, (int) $user['id']);
$instructorId = $instructor ? (int) $instructor['id'] : null;
}
$from = $request->query('from', gmdate('Y-m-d\T00:00:00\Z', strtotime('monday this week')));
$to = $request->query('to', gmdate('Y-m-d\T00:00:00\Z', strtotime('+7 day', strtotime($from))));
$payload['sunTimes'] = (new SunTimesService())->forRange($payload['tenant'], (string) $from, (string) $to);
$payload['dayMeta'] = (new HolidayService())->forRange($payload['tenant'], (string) $from, (string) $to);
$payload['appointments'] = (new AppointmentRepository())->listForTenant(
$tenantId,
(string) $from,
(string) $to,
$instructorId
);
$payload['nextAppointment'] = (new AppointmentRepository())->nextForTenant(
$tenantId,
gmdate(DATE_ATOM),
$instructorId
);
Response::json($payload);
}
}