const USERS_KEY = "inf_users_v1"; const CURR_KEY = "inf_current_v1"; function readUsers(){ return JSON.parse(localStorage.getItem(USERS_KEY) || "[]"); } function writeUsers(arr){ localStorage.setItem(USERS_KEY, JSON.stringify(arr)); } function setCurrent(email){ localStorage.setItem(CURR_KEY, email); } function getCurrent(){ const e = localStorage.getItem(CURR_KEY); return e ? readUsers().find(u=>u.email===e) : null; } function registerUser(){ const email = (document.getElementById("r_email")?.value || "").trim().toLowerCase(); const username = (document.getElementById("r_username")?.value || "").trim(); const name = (document.getElementById("r_name")?.value || "").trim(); const pass = document.getElementById("r_password")?.value || ""; const pass2 = document.getElementById("r_password2")?.value || ""; if(!email ||!username ||!name ||!pass ||!pass2){ alert("Заполните все поля"); return; } if(pass !== pass2){ alert("Пароли не совпадают"); return; } const users = readUsers(); if(users.find(u => u.email === email)){ alert("Пользователь с такой почтой уже есть"); return; } if(users.find(u => u.username === username)){ alert("Никнейм занят"); return; } const user = { email, username, name, password: pass, avatar:"", description:"", privacy:{showAvatar:true,showName:true,showDescription:true,showEmail:false}, chats:[] }; users.push(user); writeUsers(users); setCurrent(email); alert("Регистрация успешна"); location.href='chat.html'; } function loginUser(){ const email = (document.getElementById("l_email")?.value || "").trim().toLowerCase(); const pass = document.getElementById("l_password")?.value || ""; if(!email || !pass){ alert("Заполните поля"); return; } const users = readUsers(); const u = users.find(x => x.email === email && x.password === pass); if(!u){ alert("Неверная почта или пароль"); return; } setCurrent(email); location.href='chat.html'; }