Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added admin login #32

Merged
merged 1 commit into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions controllers/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use app\core\Request;
use app\core\Response;
use app\models\Admin;
use app\models\AdminLogin;
use app\models\Customer;
use app\models\CustomerLoginForm;

Expand Down Expand Up @@ -186,4 +187,28 @@ public function adminSignUp(Request $request)
]);
}

/* admin login method */
public function adminLogin(Request $request, Response $response)
{
$adminLogin = new AdminLogin();
if ($request->isPost()) {
$adminLogin->loadData($request->getBody());
if ($adminLogin->validate() && $adminLogin->login()) {
$response->redirect('/admin-dashboard'); // later will change this to admin dashboard
return;
}
}
$this->setLayout('auth');
return $this->render('/admin/admin-login', [
'model' => $adminLogin
]);
}

/* admin logout method */
public function adminLogout(Request $request, Response $response)
{
Application::$app->logoutAdmin();
$response->redirect('/');
}

}
189 changes: 189 additions & 0 deletions controllers/AuthController.php~
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
<?php

namespace app\controllers;

use app\core\Application;
use app\core\Controller;
use app\core\Request;
use app\core\Response;
use app\models\Admin;
use app\models\Customer;
use app\models\CustomerLoginForm;

//use app\core\Response;
use app\models\CustomerRegisterModel;
use app\models\ServiceCenter;
use app\models\ServiceCenterLogin;
use app\models\Technician;
use app\models\ServiceCentre;
use app\models\TechnicianLogin;

class AuthController extends Controller
{
/* customer sign up method */
public function customerSignUp(Request $request)
{
$customer = new Customer();
if ($request->isPost()) {

$customer->loadData($request->getBody());
if ($customer->validate() && $customer->save()) {
Application::$app->session->setFlash('success', 'You have been registered successfully!');
Application::$app->response->redirect('/customer-login');
}
$this->setLayout('auth');
return $this->render('/customer/customer-sign-up', [
'model' => $customer
]);
}
$this->setLayout('auth');
return $this->render('/customer/customer-sign-up', [
'model' => $customer
]);
}

/* customer login method */
public function customerLogin(Request $request, Response $response)
{
$loginForm = new CustomerLoginForm();
if ($request->isPost()) {
$loginForm->loadData($request->getBody());
if ($loginForm->validate() && $loginForm->login()) {
$response->redirect('/customer-dashboard'); // later will change this to customer dashboard
$customer = new Customer();
$customer->customerAddressGeocoding();
return;
}
}
$this->setLayout('auth');
return $this->render('/customer/customer-login', [
'model' => $loginForm
]);
}

/* customer logout method */
public function customerLogout(Request $request, Response $response)
{
Application::$app->logoutCustomer();
$response->redirect('/');
}

/* technician sign up method */
public function technicianSignUp(Request $request)
{
$technician = new Technician();
if ($request->isPost()) {
$technician->loadData($request->getBody());

if ($technician->validate() && $technician->save()) {
Application::$app->session->setFlash('success', 'You have been registered successfully!');
Application::$app->response->redirect('/technician-login');
}
$this->setLayout('auth');
return $this->render('/technician/technician-sign-up', [
'model' => $technician
]);
}
$this->setLayout('auth');
return $this->render('/technician/technician-sign-up', [
'model' => $technician
]);
}

// technician login method
public function technicianLogin(Request $request, Response $response)
{
$technicianLogin = new TechnicianLogin();
if ($request->isPost()) {
$technicianLogin->loadData($request->getBody());
if ($technicianLogin->validate() && $technicianLogin->loginTechnician()) {
$response->redirect('/technician-dashboard');
$technician = new Technician();
$technician->technicianAddressGeocoding();
return;
}
}
$this->setLayout('auth');
return $this->render('/technician/technician-login', ['model' => $technicianLogin]);
}

public function technicianLogOut(Request $request, Response $response)
{
Application::$app->logoutTechnician();
$response->redirect('/');
}


/* service centre sign up method */

public function serviceCentreSignup(Request $request)
{
$registerModel = new ServiceCenter();
if ($request->isPost()) {
$registerModel->loadData($request->getBody());

if ($registerModel->validate() && $registerModel->save()) {
Application::$app->session->setFlash('success', 'You have been registered successfully!');
Application::$app->response->redirect('/service-centre-login');
}
$this->setLayout('auth');
return $this->render('/service-centre/service-centre-sign-up', [
'model' => $registerModel
]);
}
$this->setLayout('auth');
return $this->render('/service-centre/service-centre-sign-up', [
'model' => $registerModel
]);
}

/* service centre login method */
// public function serviceCentreLogin(Request $request)
// service centre login method
public function serviceCentreLogin(Request $request, Response $response)
{
$serviceCenterLogin = new ServiceCenterLogin();
if ($request->isPost()) {
$serviceCenterLogin->loadData($request->getBody());
if ($serviceCenterLogin->validate() && $serviceCenterLogin->loginServiceCenter()) {
$response->redirect('/service-centre-dashboard');
$service_centre = new ServiceCenter();
$service_centre->serviceCentreAddressGeocoding();
return;
}
}
$this->setLayout('auth');
return $this->render('/service-centre/service-centre-login', [
'model' => $serviceCenterLogin
]);
}

public function serviceCenterLogout(Request $request, Response $response)
{
Application::$app->logoutServiceCenter();
$response->redirect('/service-centre-landing');
}

/* admin sign up method */
public function adminSignUp(Request $request)
{
$admin = new Admin();
if ($request->isPost()) {

$admin->loadData($request->getBody());
if ($admin->validate() && $admin->save()) {
Application::$app->session->setFlash('success', 'You have been registered successfully!');
Application::$app->response->redirect('/admin-login');
}
$this->setLayout('auth');
return $this->render('/admin/admin-sign-up', [
'model' => $admin
]);
}
$this->setLayout('auth');
return $this->render('/admin/admin-sign-up', [
'model' => $admin
]);
}

}
26 changes: 26 additions & 0 deletions core/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Application
public string $serviceCenterClass;
public static Application $app;
public string $customerClass;
public string $adminClass;
public Router $router;
public Request $request;
public Response $response;
Expand All @@ -27,6 +28,7 @@ public function __construct($rootPath, array $config)
$this->customerClass = $config['customerClass'];
$this->technicianClass = $config['technicianClass'];
$this->serviceCenterClass = $config['serviceCenterClass'];
$this->adminClass = $config['adminClass'];
self::$ROOT_DIR = $rootPath;
self::$app = $this;
$this->request = new Request();
Expand Down Expand Up @@ -65,6 +67,15 @@ public function __construct($rootPath, array $config)
} else {
$this->serviceCenter = null;
}

$primaryValueAdmin = $this->session->get('admin');
if ($primaryValueAdmin) {
$adminInstance = new $this->adminClass;
$primaryKey = $adminInstance->primaryKey();
$this->admin = $adminInstance->findOne([$primaryKey => $primaryValueAdmin]);
} else {
$this->admin = null;
}
}

public function loginCustomer(DbModel $customer)
Expand Down Expand Up @@ -133,4 +144,19 @@ public function logoutServiceCenter()
$this->session->remove('serviceCenter');
}

public function loginAdmin(DbModel $admin)
{
$this->admin = $admin;
$primaryKey = $admin->primaryKey();
$primaryValue = $admin->{$primaryKey};
$this->session->set('admin', $primaryValue);
return true;
}

public function logoutAdmin()
{
$this->admin = null;
$this->session->remove('admin');
}

}
46 changes: 46 additions & 0 deletions models/AdminLogin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace app\models;

use app\core\Application;
use app\core\Model;
use app\models\Admin;

class AdminLogin extends Model
{

public string $email = '';
public string $password = '';

public function rules(): array
{
return [
'email' => [self::RULE_REQUIRED, self::RULE_EMAIL],
'password' => [self::RULE_REQUIRED]
];
}

public function updateRules(): array
{
return [];
}

public function login()
{
$adminModel = new Admin();
$admin = $adminModel->findOne(['email' => $this->email]);
if (!$admin) {
$this->addErrorMessage('email', 'User does not exist with this email');
return false;
}

if (!password_verify($this->password, $admin->password)) {
$this->addErrorMessage('password', 'Password is incorrect');
return false;
}

show($admin);

return Application::$app->loginAdmin($admin);
}
}
6 changes: 5 additions & 1 deletion public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
'technicianClass' => \app\models\Technician::class,
'customerClass' => \app\models\Customer::class,
'serviceCenterClass' => \app\models\ServiceCenter::class,
'adminClass' => \app\models\Admin::class,
'db' => [
'dsn' => $_ENV['DB_DSN'],
'user' => $_ENV['DB_USER'],
Expand Down Expand Up @@ -98,8 +99,11 @@
/* Admin Auth routes */
$app->router->get('/admin-sign-up', [AuthController::class, 'adminSignUp']);
$app->router->post('/admin-sign-up', [AuthController::class, 'adminSignUp']);
$app->router->get('/admin-login', [AuthController::class, 'adminLogin']);
$app->router->post('/admin-login', [AuthController::class, 'adminLogin']);
$app->router->get('/admin-logout', [AuthController::class, 'adminLogout']);

/* routes related to the by Post */
/* Routes related to the by Post */
$app->router->get('/technician-create-post', [TechnicianController::class, 'technicianCreatePost']);
$app->router->get('/technician-edit-post', [TechnicianController::class, 'technicianEditPost']);
$app->router->post('/technician-create-post', [PostController::class, 'create']);
Expand Down
Loading