Files
breadcrumb-the-shire/pages/auth/verify().php

42 lines
1.5 KiB
PHP
Raw Normal View History

2026-02-04 23:31:53 +01:00
<?php
use MintyPHP\Buffer;
use MintyPHP\Http\SessionStoreInterface;
2026-02-04 23:31:53 +01:00
use MintyPHP\Router;
2026-02-23 12:58:19 +01:00
use MintyPHP\Service\Auth\AuthServicesFactory;
use MintyPHP\Session;
use MintyPHP\Support\Flash;
2026-02-04 23:31:53 +01:00
$sessionStore = app(SessionStoreInterface::class);
$session = $sessionStore->all();
2026-03-04 15:56:58 +01:00
$request = requestInput();
$errorBag = formErrors();
2026-02-04 23:31:53 +01:00
$errors = [];
$email = trim((string) $request->body('email', $session['password_reset_email'] ?? ''));
2026-03-04 15:56:58 +01:00
$code = trim((string) $request->body('code', ''));
$passwordResetService = (app(AuthServicesFactory::class))->createPasswordResetService();
2026-02-04 23:31:53 +01:00
2026-03-04 15:56:58 +01:00
if ($request->isMethod('POST')) {
2026-02-04 23:31:53 +01:00
if (!Session::checkCsrfToken()) {
2026-03-04 15:56:58 +01:00
$errorBag->addGlobal(t('Form expired, please try again'));
2026-02-04 23:31:53 +01:00
} elseif ($email === '' || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
2026-03-04 15:56:58 +01:00
$errorBag->addGlobal(t('Please enter a valid email address'));
2026-02-04 23:31:53 +01:00
} elseif ($code === '' || !preg_match('/^\d{6}$/', $code)) {
2026-03-04 15:56:58 +01:00
$errorBag->addGlobal(t('Please enter the 6-digit code'));
2026-02-04 23:31:53 +01:00
} else {
2026-02-23 12:58:19 +01:00
$result = $passwordResetService->verifyCode($email, $code);
2026-02-04 23:31:53 +01:00
if ($result['ok'] ?? false) {
$sessionStore->set('password_reset_id', (int) $result['reset_id']);
$sessionStore->remove('password_reset_email');
2026-02-04 23:31:53 +01:00
Flash::success(t('Code verified'), 'password/reset', 'reset_verified');
Router::redirect('password/reset');
} else {
2026-03-04 15:56:58 +01:00
$errorBag->addGlobal(t('Invalid or expired code'));
2026-02-04 23:31:53 +01:00
}
}
}
2026-03-04 15:56:58 +01:00
$errors = $errorBag->toFlatList();
2026-02-04 23:31:53 +01:00
Buffer::set('title', t('Verify code'));