").text(str ?? "").html();
}
// Comprobar estado de sesión al cargar la página
@@ -21,83 +21,75 @@ function comprobarSesion() {
$.ajax({
method: "POST",
url: "controladores/controladorUsuario.php",
+ dataType: "json",
data: { orden: "comprobarSesion" },
- }).done(function (respuesta) {
- try {
- if (typeof respuesta === "string") {
- respuesta = JSON.parse(respuesta);
- }
+ })
+ .done(function (respuesta) {
+ const contenedor = $("#contenedorUsuarioCabecera");
+ if (!contenedor.length) return; // Si aún no existe el contenedor en HTML, cancelamos
- const contenedor = $("#contenedorUsuarioCabecera");
- contenedor.empty();
+ contenedor.empty();
- if (respuesta.activa && respuesta.usuario) {
- // Recogemos el idUsuario de la sesion en una variable para usarlo (no me lo volváis a borrar por favor os lo pido)
- idUsuarioSesion = respuesta.usuario.idUsuario;
- // Obtenemos la ruta del avatar o la de por defecto
- const avatarUrl =
- respuesta.usuario.avatar || "img/avatars/defaultProfile.jpeg";
+ if (respuesta && respuesta.activa && respuesta.usuario) {
+ // Guardar idUsuario globalmente
+ idUsuarioSesion = respuesta.usuario.idUsuario;
+ const avatarUrl = respuesta.usuario.avatar || "img/avatars/defaultProfile.jpeg";
- // Usuario autenticado: Mostrar Menú Dropdown con el avatar
- const htmlUsuario = `
-
-
-
-
- `;
- contenedor.html(htmlUsuario);
-
- // Si estamos en la página principal, mostramos el botón JUGAR
- if ($("#colBotonJugar").length) {
- $("#colBotonJugar").removeClass("d-none");
- $("#colRanking")
- .removeClass("col-lg-8")
- .addClass("col-lg-7 col-xl-8");
- }
- } else {
- // No hay sesión: Mostrar botones de Login y Registro
- const htmlInvitado = `
-