JudgePole/Public/ecma/Managers/SessionManager.ecma.js
2026-09-20 17:12:09 +02:00

45 lines
1.2 KiB
JavaScript

"use strict";
const SessionManager = (function(){
const SessionManager = function(judge_pole){
const self = this;
let current_user = null;
// Comprueba si existe al menos un usuario con contraseña registrada
this.is_auth_required = () => {
const users = judge_pole.db.get_data().users || [];
return users.some(u => u.password && u.password.trim().length > 0);
};
// Si no hay usuarios con contraseña, cualquiera es admin por defecto
this.is_admin = () => {
if(!self.is_auth_required()) return true;
return current_user !== null;
};
this.get_user = () => current_user;
this.login = (nick, password) => {
const users = judge_pole.db.get_data().users || [];
const user = users.find(u =>
u.nick.trim().toLowerCase() === nick.trim().toLowerCase() &&
u.password === password
);
if(user){
current_user = user;
return true;
}
return false;
};
this.logout = () => {
current_user = null;
};
};
return SessionManager;
})();