Skip to content

Commit d69c2b9

Browse files
author
Alumno
committed
Review full interface: dynamic base routes and JS stability
1 parent b9f6201 commit d69c2b9

10 files changed

Lines changed: 56 additions & 37 deletions

File tree

public/js/main.js

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
class App {
55
constructor() {
66
this.csrfToken = this.getCsrfToken();
7+
this.appBase = this.getAppBase();
78
this.init();
89
}
910

@@ -18,16 +19,38 @@ class App {
1819
return meta ? meta.getAttribute('content') : '';
1920
}
2021

22+
getAppBase() {
23+
const rawBase = window.APP_BASE || '';
24+
if (typeof rawBase !== 'string' || rawBase === '/') {
25+
return '';
26+
}
27+
28+
return rawBase.endsWith('/') ? rawBase.slice(0, -1) : rawBase;
29+
}
30+
31+
toRoute(path) {
32+
const normalizedPath = path.startsWith('/') ? path : `/${path}`;
33+
return `${this.appBase}${normalizedPath}`;
34+
}
35+
2136
initAlerts() {
2237
const alerts = document.querySelectorAll('.alert');
2338
alerts.forEach((alert) => {
39+
if (alert.dataset.bound === '1') {
40+
return;
41+
}
42+
43+
alert.dataset.bound = '1';
2444
const closeBtn = document.createElement('button');
2545
closeBtn.className = 'alert-close';
2646
closeBtn.setAttribute('type', 'button');
2747
closeBtn.setAttribute('aria-label', 'Cerrar alerta');
2848
closeBtn.textContent = 'x';
2949
closeBtn.addEventListener('click', () => this.closeElement(alert));
30-
alert.appendChild(closeBtn);
50+
51+
if (!alert.querySelector('.alert-close')) {
52+
alert.appendChild(closeBtn);
53+
}
3154

3255
setTimeout(() => this.closeElement(alert), 5000);
3356
});
@@ -67,7 +90,7 @@ class App {
6790

6891
button.addEventListener('click', () => {
6992
this.showNotification(
70-
'La funcionalidad de cambio de contrasena se conecta desde la API /change-password.',
93+
`La funcionalidad se conecta al endpoint ${this.toRoute('/api.php?path=change-password')}.`,
7194
'info'
7295
);
7396
});
@@ -95,7 +118,7 @@ class App {
95118
const response = await fetch(url, mergedOptions);
96119

97120
if (response.status === 401) {
98-
window.location.href = '/login.php';
121+
window.location.href = this.toRoute('/login.php');
99122
return null;
100123
}
101124

views/auth/forgot-password.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
</div>
1515
<?php endif; ?>
1616

17-
<form action="/forgot-password.php" method="POST" class="auth-form" id="forgotPasswordForm">
17+
<form action="<?= $routeBase ?>/forgot-password.php" method="POST" class="auth-form" id="forgotPasswordForm">
1818
<input type="hidden" name="csrf_token" value="<?= $csrf_token ?>">
1919

2020
<div class="form-group">
@@ -38,7 +38,7 @@ class="form-control"
3838
</div>
3939

4040
<div class="form-links">
41-
<a href="/login.php">Volver al inicio de sesion</a>
41+
<a href="<?= $routeBase ?>/login.php">Volver al inicio de sesion</a>
4242
</div>
4343
</form>
4444
</div>

views/auth/login.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
</div>
1515
<?php endif; ?>
1616

17-
<form action="/login.php" method="POST" class="auth-form" id="loginForm">
17+
<form action="<?= $routeBase ?>/login.php" method="POST" class="auth-form" id="loginForm">
1818
<input type="hidden" name="csrf_token" value="<?= $csrf_token ?>">
1919

2020
<div class="form-group">
@@ -52,8 +52,8 @@ class="form-control"
5252
</div>
5353

5454
<div class="form-links">
55-
<a href="/forgot-password.php">Olvidaste tu contrasena?</a>
56-
<a href="/register.php">No tienes cuenta? Registrate</a>
55+
<a href="<?= $routeBase ?>/forgot-password.php">Olvidaste tu contrasena?</a>
56+
<a href="<?= $routeBase ?>/register.php">No tienes cuenta? Registrate</a>
5757
</div>
5858
</form>
5959
</div>

views/auth/register.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
</div>
1515
<?php endif; ?>
1616

17-
<form action="/register.php" method="POST" class="auth-form" id="registerForm">
17+
<form action="<?= $routeBase ?>/register.php" method="POST" class="auth-form" id="registerForm">
1818
<input type="hidden" name="csrf_token" value="<?= $csrf_token ?>">
1919

2020
<div class="form-group">
@@ -83,7 +83,7 @@ class="form-control"
8383
</div>
8484

8585
<div class="form-links">
86-
<a href="/login.php">Ya tienes cuenta? Inicia sesion</a>
86+
<a href="<?= $routeBase ?>/login.php">Ya tienes cuenta? Inicia sesion</a>
8787
</div>
8888
</form>
8989
</div>

views/auth/reset-password.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
</div>
1515
<?php endif; ?>
1616

17-
<form action="/reset-password.php" method="POST" class="auth-form" id="resetPasswordForm">
17+
<form action="<?= $routeBase ?>/reset-password.php" method="POST" class="auth-form" id="resetPasswordForm">
1818
<input type="hidden" name="csrf_token" value="<?= $csrf_token ?>">
1919
<input type="hidden" name="token" value="<?= htmlspecialchars($token ?? '') ?>">
2020

@@ -54,7 +54,7 @@ class="form-control"
5454
</div>
5555

5656
<div class="form-links">
57-
<a href="/login.php">Volver al inicio de sesion</a>
57+
<a href="<?= $routeBase ?>/login.php">Volver al inicio de sesion</a>
5858
</div>
5959
</form>
6060
</div>

views/dashboard/admin.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
<h2>Secure App</h2>
88
</div>
99
<ul class="nav-menu">
10-
<li><a href="/dashboard.php">Dashboard</a></li>
11-
<li><a href="/profile.php">Mi Perfil</a></li>
12-
<li><a href="/admin.php" class="active">Administracion</a></li>
13-
<li><a href="/logout.php">Cerrar Sesion</a></li>
10+
<li><a href="<?= $routeBase ?>/dashboard.php">Dashboard</a></li>
11+
<li><a href="<?= $routeBase ?>/profile.php">Mi Perfil</a></li>
12+
<li><a href="<?= $routeBase ?>/admin.php" class="active">Administracion</a></li>
13+
<li><a href="<?= $routeBase ?>/logout.php">Cerrar Sesion</a></li>
1414
</ul>
1515
</nav>
1616

@@ -38,8 +38,8 @@
3838
<div class="info-card">
3939
<h2>Acciones Rapidas</h2>
4040
<div class="action-grid">
41-
<a class="btn btn-primary btn-block" href="/api.php?path=users">Ver usuarios (API)</a>
42-
<a class="btn btn-outline btn-block" href="/dashboard.php">Volver al dashboard</a>
41+
<a class="btn btn-primary btn-block" href="<?= $routeBase ?>/api.php?path=users">Ver usuarios (API)</a>
42+
<a class="btn btn-outline btn-block" href="<?= $routeBase ?>/dashboard.php">Volver al dashboard</a>
4343
</div>
4444
</div>
4545
</div>

views/dashboard/index.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
<h2>Secure App</h2>
88
</div>
99
<ul class="nav-menu">
10-
<li><a href="/dashboard.php" class="active">Dashboard</a></li>
11-
<li><a href="/profile.php">Mi Perfil</a></li>
10+
<li><a href="<?= $routeBase ?>/dashboard.php" class="active">Dashboard</a></li>
11+
<li><a href="<?= $routeBase ?>/profile.php">Mi Perfil</a></li>
1212
<?php if ($current_user && $current_user->hasRole('admin')): ?>
13-
<li><a href="/admin.php">Administracion</a></li>
13+
<li><a href="<?= $routeBase ?>/admin.php">Administracion</a></li>
1414
<?php endif; ?>
15-
<li><a href="/logout.php">Cerrar Sesion</a></li>
15+
<li><a href="<?= $routeBase ?>/logout.php">Cerrar Sesion</a></li>
1616
</ul>
1717
</nav>
1818

views/dashboard/profile.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
<h2>Secure App</h2>
88
</div>
99
<ul class="nav-menu">
10-
<li><a href="/dashboard.php">Dashboard</a></li>
11-
<li><a href="/profile.php" class="active">Mi Perfil</a></li>
10+
<li><a href="<?= $routeBase ?>/dashboard.php">Dashboard</a></li>
11+
<li><a href="<?= $routeBase ?>/profile.php" class="active">Mi Perfil</a></li>
1212
<?php if ($current_user && $current_user->hasRole('admin')): ?>
13-
<li><a href="/admin.php">Administracion</a></li>
13+
<li><a href="<?= $routeBase ?>/admin.php">Administracion</a></li>
1414
<?php endif; ?>
15-
<li><a href="/logout.php">Cerrar Sesion</a></li>
15+
<li><a href="<?= $routeBase ?>/logout.php">Cerrar Sesion</a></li>
1616
</ul>
1717
</nav>
1818

views/layouts/footer.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
11
</div>
2-
<?php
3-
$scriptName = $_SERVER['SCRIPT_NAME'] ?? '';
4-
$assetBase = '';
5-
$publicPos = strpos($scriptName, '/public/');
6-
7-
if ($publicPos !== false) {
8-
$assetBase = substr($scriptName, 0, $publicPos + 7);
9-
}
10-
?>
2+
<?php $assetBase = $routeBase ?? ''; ?>
113
<script src="<?= $assetBase ?>/js/main.js"></script>
124
<script src="<?= $assetBase ?>/js/validation.js"></script>
135
<?= $extraJs ?? '' ?>

views/layouts/header.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,21 @@
77
<meta name="csrf-token" content="<?= $csrf_token ?? '' ?>">
88
<?php
99
$scriptName = $_SERVER['SCRIPT_NAME'] ?? '';
10-
$assetBase = '';
10+
$routeBase = '';
1111
$publicPos = strpos($scriptName, '/public/');
1212

1313
if ($publicPos !== false) {
14-
$assetBase = substr($scriptName, 0, $publicPos + 7);
14+
$routeBase = substr($scriptName, 0, $publicPos + 7);
1515
}
16+
$assetBase = $routeBase;
1617
?>
1718
<title><?= $title ?? 'Secure App' ?></title>
1819
<link rel="stylesheet" href="<?= $assetBase ?>/css/main.css">
1920
<link rel="stylesheet" href="<?= $assetBase ?>/css/forms.css">
2021
<?= $extraCss ?? '' ?>
2122
</head>
2223
<body>
24+
<script>
25+
window.APP_BASE = <?= json_encode($routeBase) ?>;
26+
</script>
2327
<div class="wrapper">

0 commit comments

Comments
 (0)