From 60f8797173f072036400f06aba0d59740a5709ed Mon Sep 17 00:00:00 2001 From: stasbilay02 Date: Wed, 4 Dec 2024 12:42:53 +0300 Subject: [PATCH] registration --- .../secure/controllers/SecureController.php | 29 +++++++++- .../secure/models/forms/RegisterForm.php | 19 +++++++ kernel/modules/secure/routs/secure.php | 2 + kernel/modules/secure/views/login.php | 7 ++- kernel/modules/secure/views/register.php | 57 +++++++++++++++++++ 5 files changed, 112 insertions(+), 2 deletions(-) create mode 100644 kernel/modules/secure/models/forms/RegisterForm.php create mode 100644 kernel/modules/secure/views/register.php diff --git a/kernel/modules/secure/controllers/SecureController.php b/kernel/modules/secure/controllers/SecureController.php index e698424..bdea8ab 100644 --- a/kernel/modules/secure/controllers/SecureController.php +++ b/kernel/modules/secure/controllers/SecureController.php @@ -7,6 +7,7 @@ use kernel\AdminController; use kernel\Flash; use kernel\helpers\Debug; use kernel\modules\secure\models\forms\LoginForm; +use kernel\modules\secure\models\forms\RegisterForm; use kernel\modules\user\service\UserService; class SecureController extends AdminController @@ -16,7 +17,6 @@ class SecureController extends AdminController protected function init(): void { parent::init(); -// $this->cgView->viewPath = KERNEL_DIR . "/views/secure/"; $this->cgView->viewPath = KERNEL_MODULES_DIR. "/secure/views/"; $this->cgView->layout = "/login.php"; $this->userService = new UserService(); @@ -61,4 +61,31 @@ class SecureController extends AdminController $this->redirect("/", code: 302); } + public function actionRegister() + { + $this->cgView->render('register.php'); + } + + public function actionRegistration(): void + { + $regForm = new RegisterForm(); + $regForm->load($_REQUEST); + + if ($this->userService->getByField('username', $regForm->getItem("username"))) { + Flash::setMessage("error", "Username already exists."); + $this->redirect("/admin/register", code: 302); + } + + if ($this->userService->getByField('email', $regForm->getItem("email"))) { + Flash::setMessage("error", "Email already exists."); + $this->redirect("/admin/register", code: 302); + } + + $user = $this->userService->create($regForm); + if ($user){ + setcookie('user_id', $user->id, time()+60*60*24, '/', $_SERVER['SERVER_NAME'], false); + $this->redirect("/admin", code: 302); + } + } + } \ No newline at end of file diff --git a/kernel/modules/secure/models/forms/RegisterForm.php b/kernel/modules/secure/models/forms/RegisterForm.php new file mode 100644 index 0000000..9d4a18e --- /dev/null +++ b/kernel/modules/secure/models/forms/RegisterForm.php @@ -0,0 +1,19 @@ + 'required|min-str-len:5|max-str-len:50', + 'email' => 'required|email|max-str-len:50', + 'password' => 'required|min-str-len:6|max-str-len:50', + ]; + } + +} \ No newline at end of file diff --git a/kernel/modules/secure/routs/secure.php b/kernel/modules/secure/routs/secure.php index 84b37c3..0377475 100644 --- a/kernel/modules/secure/routs/secure.php +++ b/kernel/modules/secure/routs/secure.php @@ -14,6 +14,8 @@ App::$collector->group(["prefix" => "admin"], function (RouteCollector $router){ App::$collector->get('/login', [\kernel\modules\secure\controllers\SecureController::class, 'actionLogin']); App::$collector->get('/logout', [\kernel\modules\secure\controllers\SecureController::class, 'actionLogout']); App::$collector->post('/auth', [\kernel\modules\secure\controllers\SecureController::class, 'actionAuth']); + App::$collector->get('/register', [\kernel\modules\secure\controllers\SecureController::class, 'actionRegister']); + App::$collector->post('/registration', [\kernel\modules\secure\controllers\SecureController::class, 'actionRegistration']); }); App::$collector->group(["prefix" => "api"], function (CgRouteCollector $router){ diff --git a/kernel/modules/secure/views/login.php b/kernel/modules/secure/views/login.php index c6cd7c3..68422b8 100644 --- a/kernel/modules/secure/views/login.php +++ b/kernel/modules/secure/views/login.php @@ -48,10 +48,15 @@ -
+ + +
diff --git a/kernel/modules/secure/views/register.php b/kernel/modules/secure/views/register.php new file mode 100644 index 0000000..973fb47 --- /dev/null +++ b/kernel/modules/secure/views/register.php @@ -0,0 +1,57 @@ + +
+ +
+
+
+ Trendy Pants and Shoes +
+
+
+ +
+ +
+ + +
+ + +
+ + +
+ + +
+ + +
+ + + + +
+ +
+
+
+
+
+ \ No newline at end of file