Create auth.js

This commit is contained in:
Voxel 2025-04-15 14:56:23 -04:00 committed by GitHub
parent 957aa30f9b
commit 2cf58ef4f2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

37
forum/auth.js Normal file
View file

@ -0,0 +1,37 @@
function getUsers() {
return JSON.parse(localStorage.getItem('users') || '{}');
}
function saveUsers(users) {
localStorage.setItem('users', JSON.stringify(users));
}
function register() {
const username = document.getElementById('reg-username').value.trim();
const password = document.getElementById('reg-password').value;
if (!username || !password) return alert("Fill in all fields");
const token = document.querySelector('[name="cf-turnstile-response"]')?.value;
if (!token) return alert("Please complete the CAPTCHA");
const users = getUsers();
if (users[username]) return alert("Username already taken");
users[username] = { password };
saveUsers(users);
localStorage.setItem('loggedInUser', username);
window.location.href = "index.html";
}
function login() {
const username = document.getElementById('login-username').value.trim();
const password = document.getElementById('login-password').value;
const users = getUsers();
if (!users[username]) return alert("User does not exist");
if (users[username].password !== password) return alert("Incorrect password");
localStorage.setItem('loggedInUser', username);
window.location.href = "index.html";
}