2026-09-03 17:35:10 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
2026-09-03 20:04:49 +01:00
|
|
|
use App\Auth\SessionPayload;
|
2026-09-03 17:35:10 +01:00
|
|
|
use App\Exception\ValidationException;
|
2026-09-03 20:04:49 +01:00
|
|
|
use App\Mail\EmailVerifier;
|
|
|
|
|
use App\Mail\MailException;
|
2026-09-03 17:35:10 +01:00
|
|
|
use App\Repository\UserRepository;
|
|
|
|
|
use Psr\Http\Message\ResponseInterface as Response;
|
|
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request;
|
|
|
|
|
|
2026-09-04 14:47:46 +01:00
|
|
|
/**
|
|
|
|
|
* Passwordless auth: there is no register/login pair. An email address is
|
|
|
|
|
* turned into an account (if it isn't one already) and sent a magic link;
|
|
|
|
|
* opening that link is what actually signs the caller in.
|
|
|
|
|
*/
|
2026-09-03 17:35:10 +01:00
|
|
|
final class AuthController extends Controller
|
|
|
|
|
{
|
|
|
|
|
private const EMAIL_MAX = 255;
|
|
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
|
private readonly UserRepository $users,
|
2026-09-03 20:04:49 +01:00
|
|
|
private readonly SessionPayload $session,
|
|
|
|
|
private readonly EmailVerifier $verifier,
|
2026-09-04 18:19:48 +01:00
|
|
|
private readonly bool $allowRegistration,
|
2026-09-03 17:35:10 +01:00
|
|
|
) {
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-04 10:29:13 +01:00
|
|
|
/**
|
|
|
|
|
* POST /api/auth/magic-link (public)
|
|
|
|
|
*
|
2026-09-04 14:47:46 +01:00
|
|
|
* Emails a one-time sign-in link for the given address, creating the
|
2026-09-04 18:19:48 +01:00
|
|
|
* account first if it doesn't exist yet -- unless registration is turned
|
|
|
|
|
* off (APP_ALLOW_REGISTRATION=false), in which case an unknown address is
|
|
|
|
|
* silently ignored and only existing users can still sign in. Always
|
|
|
|
|
* responds the same way either way, so registered addresses can't be
|
|
|
|
|
* enumerated. A link is only actually (re-)sent when one hasn't gone out
|
|
|
|
|
* in the last minute. Opening the link creates the session and, the first
|
|
|
|
|
* time, marks the address verified.
|
2026-09-04 10:29:13 +01:00
|
|
|
*/
|
|
|
|
|
public function requestLoginLink(Request $request, Response $response): Response
|
|
|
|
|
{
|
|
|
|
|
$body = (array) ($request->getParsedBody() ?? []);
|
|
|
|
|
$email = is_string($body['email'] ?? null) ? mb_strtolower(trim($body['email'])) : '';
|
|
|
|
|
|
|
|
|
|
if ($email === '' || !filter_var($email, FILTER_VALIDATE_EMAIL) || strlen($email) > self::EMAIL_MAX) {
|
|
|
|
|
throw new ValidationException(['email' => ['Enter a valid email address.']]);
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-04 18:19:48 +01:00
|
|
|
$user = $this->allowRegistration
|
|
|
|
|
? $this->users->findOrCreateByEmail($email)
|
|
|
|
|
: $this->users->findByEmail($email);
|
2026-09-04 14:47:46 +01:00
|
|
|
|
2026-09-04 18:19:48 +01:00
|
|
|
if ($user !== null && !$this->recentlyEmailed($user)) {
|
2026-09-04 10:29:13 +01:00
|
|
|
try {
|
|
|
|
|
$this->verifier->sendLoginLink($user);
|
|
|
|
|
} catch (MailException $e) {
|
|
|
|
|
error_log('Login link failed for user ' . $user['id'] . ': ' . $e->getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->json($response, [
|
2026-09-04 14:47:46 +01:00
|
|
|
'message' => 'Check your email for a link to sign in.',
|
2026-09-04 10:29:13 +01:00
|
|
|
], 202);
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-03 17:35:10 +01:00
|
|
|
/**
|
|
|
|
|
* GET /api/me (requires AuthMiddleware)
|
|
|
|
|
*/
|
|
|
|
|
public function me(Request $request, Response $response): Response
|
|
|
|
|
{
|
2026-09-03 20:04:49 +01:00
|
|
|
return $this->json($response, ['user' => $this->session->present($this->user($request))]);
|
2026-09-03 17:35:10 +01:00
|
|
|
}
|
|
|
|
|
|
2026-09-04 10:29:13 +01:00
|
|
|
/**
|
|
|
|
|
* @param array{verification_email_sent_at?: string|null} $user
|
|
|
|
|
*/
|
|
|
|
|
private function recentlyEmailed(array $user): bool
|
|
|
|
|
{
|
|
|
|
|
$lastSent = $user['verification_email_sent_at'] ?? null;
|
|
|
|
|
|
|
|
|
|
return $lastSent !== null
|
|
|
|
|
&& (time() - (int) strtotime($lastSent)) < EmailVerifier::RESEND_INTERVAL_SECONDS;
|
|
|
|
|
}
|
2026-09-03 17:35:10 +01:00
|
|
|
}
|