microserviciile de register, login finalizate
This commit is contained in:
Regular → Executable
+87
-69
@@ -1,70 +1,88 @@
|
||||
<?php
|
||||
|
||||
namespace app\controllers;
|
||||
use app\core\middlewares\AuthMiddleware;
|
||||
use app\core\Application;
|
||||
use app\core\Controller;
|
||||
use app\core\Request;
|
||||
use app\core\Response;
|
||||
use app\models\LoginForm;
|
||||
use app\models\User;
|
||||
|
||||
class AuthController extends Controller{
|
||||
public function __construct(){
|
||||
$this->registerMiddleware(new AuthMiddleware(['profile']));
|
||||
}
|
||||
|
||||
public function login(Request $request, Response $response){
|
||||
$loginForm = new LoginForm();
|
||||
|
||||
if($request->isPost()){
|
||||
$loginForm->loadData($request->getBody());
|
||||
if($loginForm->validate() && $loginForm->login()){
|
||||
$response->redirect('home');
|
||||
exit;
|
||||
}
|
||||
|
||||
$this->setLayout('auth');
|
||||
return $this->render('login', [
|
||||
'model' => $loginForm
|
||||
]);
|
||||
}
|
||||
|
||||
$this->setLayout('auth');
|
||||
return $this->render('login', [
|
||||
'model' => $loginForm
|
||||
]);
|
||||
}
|
||||
|
||||
public function register(Request $request, $response){
|
||||
$user = new User();
|
||||
|
||||
if($request->isPost()){
|
||||
$user->loadData($request->getBody());
|
||||
|
||||
if($user->validate() && $user->save()){
|
||||
$response->redirect('home');
|
||||
exit;
|
||||
}
|
||||
|
||||
$this->setLayout('auth');
|
||||
return $this->render('register', [
|
||||
'model'=>$user
|
||||
]);
|
||||
}
|
||||
|
||||
$this->setLayout('auth');
|
||||
return $this->render('register', [
|
||||
'model'=>$user
|
||||
]);
|
||||
}
|
||||
|
||||
public function logout($request, $response){
|
||||
Application::$app->logout();
|
||||
$response->redirect('/');
|
||||
}
|
||||
|
||||
public function profile(){
|
||||
return $this->render('profile');
|
||||
}
|
||||
<?php
|
||||
|
||||
namespace app\controllers;
|
||||
use app\core\Application;
|
||||
use app\core\Controller;
|
||||
use app\core\Request;
|
||||
use app\models\LoginModel;
|
||||
use app\core\Model;
|
||||
use app\models\RegisterModel;
|
||||
|
||||
class AuthController extends Controller{
|
||||
public function __construct(){
|
||||
}
|
||||
|
||||
public static function loginPage(Request $request): string
|
||||
{
|
||||
$model = new LoginModel();
|
||||
|
||||
if($request->isPost()){
|
||||
$model->loadData($request->getPOSTBody());
|
||||
$model->checkForErrors();
|
||||
|
||||
if($model->hasErrors()){
|
||||
self::setControllerParams("Login", "Login", "Auth", $model);
|
||||
return Application::$app->controller->render();
|
||||
}
|
||||
$model->removeErrors();
|
||||
|
||||
$body = $model->createBody();
|
||||
$headers = $model->createHeaders(json_encode($body));
|
||||
|
||||
$model->setParametersForRequest("GET", $headers, $body);
|
||||
|
||||
$model->execute();
|
||||
if($model->isRequestFulfilled()){
|
||||
//Application::$app->response->redirect("homepage");
|
||||
}
|
||||
|
||||
$model->setRequestError();
|
||||
self::setControllerParams("Login", "Login", "Auth", $model);
|
||||
return Application::$app->controller->render();
|
||||
}
|
||||
|
||||
self::setControllerParams("Login", "Login", "Auth", $model);
|
||||
return Application::$app->controller->render();
|
||||
}
|
||||
|
||||
public static function registerPage(Request $request): string
|
||||
{
|
||||
$model = new RegisterModel();
|
||||
|
||||
if($request->isPost()){
|
||||
$model->loadData($request->getPOSTBody());
|
||||
$model->checkForErrors();
|
||||
|
||||
if($model->hasErrors()){
|
||||
self::setControllerParams("Register", "Register", "Auth", $model);
|
||||
return Application::$app->controller->render();
|
||||
}
|
||||
$model->removeErrors();
|
||||
|
||||
$body = $model->createBody();
|
||||
$headers = $model->createHeaders(json_encode($body));
|
||||
|
||||
$model->setParametersForRequest("PUT", $headers, $body);
|
||||
|
||||
$model->execute();
|
||||
if($model->isRequestFulfilled()){
|
||||
Application::$app->response->redirect("login");
|
||||
}
|
||||
|
||||
$model->setRequestError();
|
||||
self::setControllerParams("Register", "Register", "Auth", $model);
|
||||
return Application::$app->controller->render();
|
||||
}
|
||||
|
||||
self::setControllerParams("Register", "Register", "Auth", $model);
|
||||
return Application::$app->controller->render();
|
||||
}
|
||||
|
||||
public static function setControllerParams(string $title, string $view, string $layout, ?Model $model): void
|
||||
{
|
||||
Application::$app->controller->setLayout($layout);
|
||||
Application::$app->controller->setModel($model);
|
||||
Application::$app->controller->setTitle($title);
|
||||
Application::$app->controller->setView($view);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user