63 lines
1.7 KiB
PHP
63 lines
1.7 KiB
PHP
<?php
|
|
/**
|
|
* @package Bicycle
|
|
* @author Egor Isaev
|
|
* @description LoginController.php
|
|
* @copyright (c) 06/08/2026
|
|
*/
|
|
|
|
namespace App\Controller;
|
|
|
|
use Services\Auth;
|
|
use System\Classes\Controller;
|
|
use System\Classes\HTTP\HTTPException;
|
|
use System\Classes\HTTP\Request as HTTPRequest;
|
|
use System\Classes\Request;
|
|
use System\Classes\Validation;
|
|
|
|
/**
|
|
* Вход/выход. URL: /login (форма + обработка), /login/logout.
|
|
*/
|
|
class LoginController extends Controller
|
|
{
|
|
/**
|
|
* Показывает форму входа (GET) и обрабатывает её (POST).
|
|
*
|
|
* @return string
|
|
* @throws \System\Classes\MyException
|
|
*/
|
|
public function indexAction(): string
|
|
{
|
|
$request = Request::$current;
|
|
$error = null;
|
|
|
|
if ($request->method() === HTTPRequest::POST) {
|
|
$validation = Validation::factory($request->post())
|
|
->label('login', 'Логин')
|
|
->label('password', 'Пароль')
|
|
->rule('login', 'required')
|
|
->rule('password', 'required');
|
|
|
|
if ($validation->check() && Auth::instance()->login($request->post('login'), $request->post('password'))) {
|
|
HTTPException::factory(302, '/admin/logs')->getResponse();
|
|
}
|
|
|
|
$error = 'Неверный логин или пароль';
|
|
}
|
|
|
|
return $this->render('login', ['error' => $error]);
|
|
}
|
|
|
|
/**
|
|
* Разлогинивает пользователя и возвращает на главную.
|
|
*
|
|
* @return string
|
|
*/
|
|
public function logoutAction(): string
|
|
{
|
|
Auth::instance()->logout();
|
|
|
|
HTTPException::factory(302, '/')->getResponse();
|
|
}
|
|
}
|