51 lines
1.3 KiB
PHP
51 lines
1.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
use App\Core\Auth;
|
|
use App\Repositories\AdminRepository;
|
|
|
|
require_once dirname(__DIR__, 2) . '/app/bootstrap.php';
|
|
|
|
$repository = new AdminRepository();
|
|
$needsSetup = $repository->activeCount() === 0;
|
|
|
|
if ($needsSetup) {
|
|
header('Location: /admin/setup.php?token=' . rawurlencode((string) app_env('INSTALL_TOKEN', '')));
|
|
exit;
|
|
}
|
|
|
|
$error = null;
|
|
$next = admin_safe_next((string) ($_GET['next'] ?? '/admin/'));
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$next = admin_safe_next((string) ($_POST['next'] ?? '/admin/'));
|
|
|
|
if (Auth::attempt((string) ($_POST['email'] ?? ''), (string) ($_POST['password'] ?? ''))) {
|
|
header('Location: ' . ($next !== '' ? $next : '/admin/'));
|
|
exit;
|
|
}
|
|
|
|
$error = 'Неверный email или пароль.';
|
|
}
|
|
|
|
view('admin/login', [
|
|
'title' => 'Вход в админку - Рыбсток',
|
|
'breadcrumbs' => [
|
|
['title' => 'Главная', 'url' => '/'],
|
|
['title' => 'Админка', 'url' => '/admin/'],
|
|
['title' => 'Вход'],
|
|
],
|
|
'error' => $error,
|
|
'next' => $next,
|
|
]);
|
|
|
|
function admin_safe_next(string $next): string
|
|
{
|
|
if ($next === '' || !str_starts_with($next, '/') || str_starts_with($next, '//')) {
|
|
return '/admin/';
|
|
}
|
|
|
|
return $next;
|
|
}
|