From 6f41d8eaf13b152ae3ef9695692fb83d8c10b191 Mon Sep 17 00:00:00 2001 From: Anaru <98413596+FunnyEivske@users.noreply.github.com> Date: Mon, 24 Nov 2025 18:00:43 +0100 Subject: [PATCH] Implement email/password login and validation Updated login functionality to use email and password authentication instead of anonymous sign-in. Added validation for empty fields and improved error handling. --- login.js | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/login.js b/login.js index c3e5c38..a8ec72a 100644 --- a/login.js +++ b/login.js @@ -1,28 +1,47 @@ import { initializeApp } from "https://www.gstatic.com/firebasejs/12.6.0/firebase-app.js"; -import { getAuth, signInAnonymously } from "https://www.gstatic.com/firebasejs/12.6.0/firebase-auth.js"; +import { getAuth, signInWithEmailAndPassword } from "https://www.gstatic.com/firebasejs/12.6.0/firebase-auth.js"; import { firebaseConfig } from "./config.js"; // Init const app = initializeApp(firebaseConfig); const auth = getAuth(app); -// Håndter klikk +// Håndter klikk på Logg Inn const loginBtn = document.getElementById('btn-login'); + if (loginBtn) { loginBtn.addEventListener('click', async () => { + const email = document.getElementById('inp-email').value; + const password = document.getElementById('inp-password').value; const msg = document.getElementById('error-msg'); - loginBtn.textContent = "Autentiserer..."; + // Enkel sjekk at feltene ikke er tomme + if (!email || !password) { + msg.textContent = "Du må skrive inn både e-post og passord."; + return; + } + + loginBtn.textContent = "Sjekker..."; msg.textContent = ""; try { - await signInAnonymously(auth); + // Prøver å logge inn med e-post og passord + await signInWithEmailAndPassword(auth, email, password); + // Suksess -> Gå til hovedsiden window.location.href = "Røsstad.html"; + } catch (error) { console.error(error); - msg.textContent = "Feil: " + error.message; - loginBtn.textContent = "Prøv igjen"; + // Gi en forståelig feilmelding + if (error.code === 'auth/invalid-credential' || error.code === 'auth/user-not-found' || error.code === 'auth/wrong-password') { + msg.textContent = "Feil e-post eller passord."; + } else if (error.code === 'auth/too-many-requests') { + msg.textContent = "For mange forsøk. Prøv igjen senere."; + } else { + msg.textContent = "Feil: " + error.message; + } + loginBtn.textContent = "Logg Inn"; } }); }