Moved authentification code into js/auth.js
This commit is contained in:
8 files changed
+330
-364
No files matched your search
+44
-61
@@ -154,51 +154,8 @@
|
||||
</div>
|
||||
<div id="toast" class="toast"></div>
|
||||
|
||||
<script src="js/auth.js"></script>
|
||||
<script>
|
||||
const API_BASE = 'http://127.0.0.1:5000/api';
|
||||
|
||||
function showToast(msg, isError = false) {
|
||||
const toast = document.getElementById('toast');
|
||||
toast.textContent = msg;
|
||||
toast.style.backgroundColor = isError ? 'var(--red-7)' : 'var(--green-7)';
|
||||
toast.style.display = 'block';
|
||||
setTimeout(() => { toast.style.display = 'none'; }, 3000);
|
||||
}
|
||||
|
||||
async function login(username, password) {
|
||||
try {
|
||||
const res = await fetch(`${API_BASE}/login`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ username, password }),
|
||||
credentials: 'include'
|
||||
});
|
||||
const data = await res.json();
|
||||
if (!res.ok) throw new Error(data.error || 'Login failed');
|
||||
showToast(`Welcome, ${data.username}!`);
|
||||
window.location.href = 'map-page.html';
|
||||
} catch (err) {
|
||||
showToast(err.message, true);
|
||||
}
|
||||
}
|
||||
|
||||
async function register(username, password) {
|
||||
try {
|
||||
const res = await fetch(`${API_BASE}/register`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ username, password }),
|
||||
credentials: 'include'
|
||||
});
|
||||
const data = await res.json();
|
||||
if (!res.ok) throw new Error(data.error || 'Registration failed');
|
||||
showToast(`Registered as ${data.username}. Logging in...`);
|
||||
window.location.href = 'map-page.html';
|
||||
} catch (err) {
|
||||
showToast(err.message, true);
|
||||
}
|
||||
}
|
||||
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
// Tab switching
|
||||
document.querySelectorAll('.auth-tab').forEach(tab => {
|
||||
@@ -211,30 +168,56 @@
|
||||
});
|
||||
});
|
||||
// Login button
|
||||
document.getElementById('login-submit').addEventListener('click', () => {
|
||||
document.getElementById('login-submit').addEventListener('click', async () => {
|
||||
const username = document.getElementById('login-username').value.trim();
|
||||
const password = document.getElementById('login-password').value;
|
||||
if (!username || !password) {
|
||||
showToast('Please enter username and password', true);
|
||||
return;
|
||||
}
|
||||
login(username, password);
|
||||
try {
|
||||
const res = await fetch(`${API_BASE}/login`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ username, password }),
|
||||
credentials: 'include'
|
||||
});
|
||||
const data = await res.json();
|
||||
if (!res.ok) throw new Error(data.error || 'Login failed');
|
||||
showToast(`Welcome, ${data.username}`);
|
||||
window.location.href = 'map-page.html';
|
||||
} catch (err) {
|
||||
showToast(err.message, true);
|
||||
}
|
||||
});
|
||||
// Register button
|
||||
document.getElementById('register-submit').addEventListener('click', () => {
|
||||
const username = document.getElementById('register-username').value.trim();
|
||||
const password = document.getElementById('register-password').value;
|
||||
if (!username || !password) {
|
||||
showToast('Please enter username and password', true);
|
||||
return;
|
||||
}
|
||||
if (password.length < 4) {
|
||||
showToast('Password must be at least 4 characters', true);
|
||||
return;
|
||||
}
|
||||
register(username, password);
|
||||
});
|
||||
});
|
||||
document.getElementById('register-submit').addEventListener('click', async () => {
|
||||
const username = document.getElementById('register-username').value.trim();
|
||||
const password = document.getElementById('register-password').value;
|
||||
if (!username || !password) {
|
||||
showToast('Please enter username and password', true);
|
||||
return;
|
||||
}
|
||||
if (password.length < 4) {
|
||||
showToast('Password must be at least 4 characters', true);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const res = await fetch(`${API_BASE}/register`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ username, password }),
|
||||
credentials: 'include'
|
||||
});
|
||||
const data = await res.json();
|
||||
if (!res.ok) throw new Error(data.error || 'Registration failed');
|
||||
showToast(`Registered as ${data.username}. Logging in...`);
|
||||
window.location.href = 'map-page.html';
|
||||
} catch (err) {
|
||||
showToast(err.message, true);
|
||||
}
|
||||
});
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
</html>
|
||||
Reference in new issue
Block a user