Skip to content

Commit

Permalink
Feat/auth router improvements (#80)
Browse files Browse the repository at this point in the history
Nova estructura, modificacions al enrutador, linting (laravel pint) +
exemple complet CRUD.
  • Loading branch information
0x1026 authored Nov 18, 2024
2 parents ad45900 + 83c9551 commit e083f2d
Show file tree
Hide file tree
Showing 76 changed files with 1,718 additions and 681 deletions.
10 changes: 5 additions & 5 deletions docker/database/start-scripts/1-seed.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ INSERT INTO roles (name) VALUES
('Administrador'),
('Gerente'),
('Trabajador');
INSERT INTO workers (company, name, dni, password, email, role_id, created_at, updated_at, deleted_at) VALUES
('TechCorp', 'Carlos García', '12345678A', 'hashedpassword1', '[email protected]', 1, NOW(), NOW(), NULL),
('InnovaTech', 'Ana Martínez', '23456789B', 'hashedpassword2', '[email protected]', 2, NOW(), NOW(), NULL),
('DesignWorks', 'José Rodríguez', '34567890C', 'hashedpassword3', '[email protected]', 3, NOW(), NOW(), NULL);
INSERT INTO workers (company, name, dni, password, email, role_id) VALUES
('TechCorp', 'Carlos García', '12345678A', 'hashedpassword1', '[email protected]', 1),
('InnovaTech', 'Ana Martínez', '23456789B', 'hashedpassword2', '[email protected]', 2),
('DesignWorks', 'José Rodríguez', '34567890C', 'hashedpassword3', '[email protected]', 3);
INSERT INTO contracts (name, start_date, end_date, invoice_proposed, invoice_agreed, invoice_paid) VALUES
('Ayuntamiento de Valencia', '2021-01-01', '2021-12-31', 1000.00, 900.00, 900.00),
('Administración General del Estado', '2021-01-01', '2021-12-31', 2000.00, 1800.00, 1800.00),
Expand Down Expand Up @@ -50,7 +50,7 @@ INSERT INTO incidences (name, element_id, description) VALUES
('Banco pintado', 2, 'Banco pintado con grafitis'),
('Fuente con fuga', 3, 'Fuente con fuga de agua');
--TODO: tasks, routes and works
INSERT INTO work_orders (contract_id) VALUES
INSERT INTO work_orders (contract_id) VALUES
(1),
(2),
(3);
Expand Down
66 changes: 66 additions & 0 deletions src/app/Controllers/AuthController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace App\Controllers;

use App\Core\Session;
use App\Core\View;
use App\Models\User;

class AuthController
{
public function index()
{
View::render([
'view' => 'Auth/Login',
'title' => 'Login Page',
'layout' => 'AuthLayout',
'data' => [
'error' => Session::get('error'),
],
]);

// Clear the error message after displaying it
Session::remove('error');
}

public function login($postData)
{
$email = $postData['email'] ?? null;
$password = $postData['password'] ?? null;

if (! $email || ! $password) {
// Redirect back with error if fields are missing
Session::set('error', 'Email and password are required.');
header('Location: /auth/login');
exit;
}

// Check if the user exists and password matches
$user = User::findBy(['email' => $email, 'password' => $password], true);

if (! $user || strcmp($user->password, $password) !== 0) { // TODO: Verify hashed password not raw password
echo 'Invalid email or password.';
// Redirect back with error if authentication fails
Session::set('error', 'Invalid email or password.');
header('Location: /auth/login');
exit;
}

Session::set('user', [
'id' => $user->getId(),
'name' => $user->name,
'email' => $user->email,
'role_id' => $user->role_id,
]);

header('Location: /');
exit;
}

public function logout()
{
Session::destroy();
header('Location: /auth/login');
exit;
}
}
18 changes: 0 additions & 18 deletions src/app/Controllers/CAuth.php

This file was deleted.

20 changes: 0 additions & 20 deletions src/app/Controllers/CContract.php

This file was deleted.

20 changes: 0 additions & 20 deletions src/app/Controllers/CElement.php

This file was deleted.

20 changes: 0 additions & 20 deletions src/app/Controllers/CHome.php

This file was deleted.

45 changes: 0 additions & 45 deletions src/app/Controllers/CIncidence.php

This file was deleted.

20 changes: 0 additions & 20 deletions src/app/Controllers/CPruningType.php

This file was deleted.

20 changes: 0 additions & 20 deletions src/app/Controllers/CTaskType.php

This file was deleted.

20 changes: 0 additions & 20 deletions src/app/Controllers/CTreeType.php

This file was deleted.

60 changes: 0 additions & 60 deletions src/app/Controllers/CWorkOrder.php

This file was deleted.

20 changes: 0 additions & 20 deletions src/app/Controllers/CZone.php

This file was deleted.

20 changes: 20 additions & 0 deletions src/app/Controllers/ContractController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App\Controllers;

use App\Core\View;
use App\Models\Contract;

class ContractController
{
public function index()
{
$contracts = Contract::findAll();
View::render([
'view' => 'Contracts',
'title' => 'Contracts',
'layout' => 'MainLayout',
'data' => ['contracts' => $contracts],
]);
}
}
Loading

0 comments on commit e083f2d

Please sign in to comment.